Integrating Google Sign-In into Your Android App
To integrate Google Sign-In into your Android app, configure Google Sign-In and add a button to your app's layout that starts the sign-in flow.
Before you begin
Configure a Google Developers Console project and set up your Android Studio project.
Add the configuration file to your project
Copy the google-services.json file you just downloaded into the app/ or mobile/ directory of your Android Studio project. Open the Android Studio Terminal pane:
$ mv path-to-download/google-services.json app/
Add the Google Services plugin
The Google Services plugin for Gradle parses configuration information from the google-services.json file. Add the plugin to your project by updating your top-level build.gradle and your app-level build.gradle files as follows:
- Add the dependency to your project-level
build.gradle:classpath 'com.google.gms:google-services:2.0.0-alpha6'
- Add the plugin to your app-level
build.gradle:apply plugin: 'com.google.gms.google-services'
Configure Google Sign-In and the GoogleApiClient object
In your sign-in activity's
onCreatemethod, configure Google Sign-In to request the user data required by your app. For example, to configure Google Sign-In to request users' ID and basic profile information, create aGoogleSignInOptionsobject with theDEFAULT_SIGN_INparameter. To request users' email addresses as well, create theGoogleSignInOptionsobject with therequestEmailoption.If you need to request additional scopes to access Google APIs, specify them with
requestScopes.Then, also in your sign-in activity's
onCreatemethod, create aGoogleApiClientobject with access to the Google Sign-In API and the options you specified.Note: To use
enableAutoManage, your activity must extendFragmentActivityorAppCompatActivity(a subclass ofFragmentActivity), both of which are part of the Android Support Library. You can useGoogleApiClientin aFragment; however, the fragment's parent activity must be aFragmentActivity. If you can't extendFragmentActivity, you must manually manage theGoogleApiClientconnection lifecycle.
Add the Google Sign-In button to your app
Add the SignInButtonin your application's layout:<com.google.android.gms.common.SignInButton
android:id="@+id/sign_in_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />Optional: If you are using the default sign-in button graphic instead of providing your own sign-in button assets, you can customize the button's size and color scheme with the
setSizeandsetScopesmethods. Also, if you specify a Google+ social scope tosetScopes, the sign-in button will be rendered with the red Google+ branding.In the Android activity (for example, in the
onCreatemethod), register your button'sOnClickListenerto sign in the user when clicked:findViewById(R.id.sign_in_button).setOnClickListener(this);
Start the sign-in flow
In the activity's onClickmethod, handle sign-in button taps by creating a sign-in intent with thegetSignInIntentmethod, and starting the intent withstartActivityForResult.@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sign_in_button:
signIn();
break;
// ...
}
}Starting the intent prompts the user to select a Google account to sign in with. If you requested scopes beyond
profile,email, andopenid, the user is also prompted to grant access to the requested resources.In the activity's
onActivityResultmethod, retrieve the sign-in result withgetSignInResultFromIntent.After you retrieve the sign-in result, you can check if sign-in succeeded with the
isSuccessmethod. If sign-in succeeded, you can call thegetSignInAccountmethod to get aGoogleSignInAccountobject that contains information about the signed-in user, such as the user's name.You can also get the user's email address with
getEmail, the user's Google ID (for client-side use) withgetId, and an ID token for the user with withgetIdToken. If you need to pass the currently signed-in user to a backend server, send the ID token to your backend server and validate the token on the server.
Cross-platform single sign on
You can use silentSignIn to automatically sign the user in if the user previously granted authorization to your app on another platform. This allows the user to be signed in to your app immediately, if your project clients are configured to meet the following requirements:
- The OAuth 2.0 client IDs must be in the same Google Developers Console project.
- The OAuth scopes must be the same in both clients.
If the user signed in to your web app previously, silent sign-in succeeds. You can proceed to access Google APIs to retrieve the user's info and bypass the need for the user to sign in to your app again.
Localization
The SDK provides localized strings for the com.google.android.gms.common.SignInButton button and these are automatically available to users of your app. To view a full list of languages, you can examine the following directory in the SDK: <android-sdk-folder>/extras/google/google_play_services/libproject/google-play-services_lib/res/. In that location, you will find directories named values-<langcode>.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 3.0 License, and code samples are licensed under the Apache 2.0 License. For details, see our Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Integrating Google Sign-In into Your Android App的更多相关文章
- android app性能优化大汇总
这里根据网络上各位大神已经总结的知识内容做一个大汇总,作为记录,方便后续“温故知新”. 性能指标: (1)使用流畅度: 图片处理器每秒刷新的帧数(FPS),可用来指示页面是否平滑的渲染.高的帧率可以 ...
- 谷歌发布基于机器学习的Android APP安全检测系统:Google Play Protect
Google Play作为众所周知的在线应用市场,因为审查制度的松散,经常会有一些恶意软件伪装成其他应用混入其中.此前阿里聚安全小编就报道了2例关于恶意软件伪装在Google Play上的事件:< ...
- Android App在Google App Store中搜不到
情景:Android App在Google App Store上架成功,三星手机可以在Google App Store中搜索到,但是三星tablet却无法在Google App Store中搜索到,目 ...
- Unity上线google商店 用IL2Cpp打包64位版本和Android APP Bundle优化 及产生的bug
ios刚上线,这边着手改成android版本,我开始使用的是unity2017.4.1版本 上传谷歌商店是出现这两个警告: 要支持64位,但是在2017版本上没有找到64位的打包选项,猜测应该是版本的 ...
- 【Unity3D】Android App Bundle(aab)打包上架Google Play介绍
总体说来,Android App Bundle打包有3种方式,每种方式都有成功上架Google Play进行测试通过,因此实用程度还是挺高的.能够理解以下内容的前提是会打apk包,知道如何生成Asse ...
- 第三篇:gradle 编译 Android app 概览
引言:经过上两篇的论述,我们已经从代码到架构都简单的熟悉了一遍,理论上,只要知道android app的编译过程,我们大可以自己写一份用gradle编译app的插件,插件内将将整个流程用Task的依赖 ...
- MAC安裝《Genymotion Android模擬器》大玩Android APP (神魔之塔)
链接地址:http://www.minwt.com/mac/10083.html/comment-page-2 MAC» 智慧型裝罝» Android | 2014/02/12 Android是一個開 ...
- google+ sign in and get the oauth token 转摘:https://gist.github.com/ianbarber/5170508
package com.example.anothersignintest; import java.io.IOException; import com.google.android.gms ...
- Migrating Your Android App from Eclipse to Android Studio
By: Jason Snell | Posted in: Mobile, Performance Tech Tips, Top Post, Jun. 7th, 2013 12 inShare One ...
随机推荐
- 图论:费用流-SPFA+EK
利用SPFA+EK算法解决费用流问题 例题不够裸,但是还是很有说服力的,这里以Codevs1227的方格取数2为例子来介绍费用流问题 这个题难点在建图上,我感觉以后还要把网络流建模想明白才能下手去做这 ...
- Spring - IoC(6): 作用域
Spring 支持五种作用域,分别是 singleton.prototype.request.session 和 global session. 作用域 说明 singleton (默认作用域)单例 ...
- Liberty中应用的contextroot
参考:http://www-01.ibm.com/support/knowledgecenter/api/content/SSEQTP_8.5.5/com.ibm.websphere.wlp.doc/ ...
- PCIe 调试
ISE 生成PCIe核之后, 在ipcore_dir目录下会产生以下文件目录 目录下包含内容如下: The doc folder contains the PCIe Endpoint Block da ...
- AtCoder Regular Contest 075 D Widespread
题目传送门 这道题其实二分一下答案就okay了的 不过LL什么的有时候忘了加 被卡了下 #include<cstdio> #include<cstring> #include& ...
- JVM指令的使用深入详解
原文地址:https://www.jb51.net/article/155293.htm 一.未归类系列A 此系列暂未归类. 指令码 助记符 ...
- Linux下安装mantis配置指南【转】
转自:http://blog.csdn.net/xabc3000/article/details/6858229 目录(?)[-] Linux下安装mantis配置指南 配置Linux下的Apache ...
- 关于EINTR错误的理解【转】
转自:http://www.xuebuyuan.com/1470645.html 最近在工作中遇到了EINTR错误,感到比较困惑,几番研究之后,颇有心得和收获,特记录如下,便于以后查询,也给有同样困惑 ...
- 8.read读取控制台输入
read(选项)(参数)选项:-p:指定读取值时的提示符-t:指定读取时等待的时间(秒),如果没有在指定的时间内输入,就不再等待了参数:变量:指定读取时的变量名
- 《Java编程思想》笔记 第七章 复用类
1.组合 将其他类的对象引用置于新的类中. 3.继承 extends 从已知的一个类中派生出新的一个类,叫子类.子类实现了父类所有 非私有化 非静态 的属性和方法,并能根据自己的实际需求扩展出新的行为 ...