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 ...
随机推荐
- 【20160811】noip模拟-未完
T1 T2 T3 小奇回地球 [问题描述] 简单来说,它要从标号为1的星球到标号为n的星球,某一些星球之间有航线.由于超时空隧道的存在,从一个星球到另一个星球时间可能会倒流,而且,从星球a到b耗费的时 ...
- CSS3 渐变(Gradients)
参考: http://www.runoob.com/css3/css3-gradients.html CSS3 渐变(gradients)可以让你在两个或多个指定的颜色之间显示平稳的过渡. 以前,你必 ...
- C/C++里的const(2)
对于如下几个语句,哪些定义相同?哪些定义不同?哪些数据可修改?哪些数据不可修改呢? const int a; int const a; const int *a; int *const a; int ...
- 24式太极拳:3D动画演示(图文)
http://blog.sina.com.cn/s/blog_4be33b740102e9ae.html 24式太极拳:3D动画演示(图文) (2013-03-10 18:45:55) 转载▼ 标签: ...
- 常见协议基础知识总结--FTP协议
FTP协议是一种基于客户端和服务器的文件传输协议,属于应用层协议,基于传输层的TCP协议: FTP主要分成主动模式和被动模式两种传输方式, 方式是相对服务器而言的,服务器主动发起数据连接即主动方式,使 ...
- [ Openstack ] Openstack-Mitaka 高可用之 memcache
目录 Openstack-Mitaka 高可用之 概述 Openstack-Mitaka 高可用之 环境初始化 Openstack-Mitaka 高可用之 Mariadb-Galera集群 ...
- heightCharts改变图表内图标的样式:链接外部图片
series: [{ name: '东京', marker: { symbol: 'url(https://www.highcharts.com/demo/gfx/sun.png)' //链接 ...
- 使用echarts展示线状图信息的时候数据部分数据因为x轴的数据显示不全而隐藏的问题
在使用echarts来展示数据时,因为数据很多的原因导致x轴显示不全,然后有些数据也隐藏在图表中,所以这个时候我们要在 series 中设置一个属性,让所有的数据都能够展示出来,这里我们需要添加的属性 ...
- BZOJ 1207
1207: [HNOI2004]打鼹鼠 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3089 Solved: 1499[Submit][Statu ...
- 定义序列化器时的read_only和write_only选项
# 转载请留言联系 read_only read_only表示只能读,不能进行修改.例如定义序列化器时,id字段通常指定read_only=True.在序列化时,即对象转为字典.JSON字符串时,字典 ...