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:

MAC/LINUXWINDOWS
$ 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:

  1. Add the dependency to your project-level build.gradle:

    classpath 'com.google.gms:google-services:2.0.0-alpha6'
  2. Add the plugin to your app-level build.gradle:
    apply plugin: 'com.google.gms.google-services'

Configure Google Sign-In and the GoogleApiClient object

  1. In your sign-in activity's onCreate method, 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 aGoogleSignInOptions object with the DEFAULT_SIGN_IN parameter. To request users' email addresses as well, create the GoogleSignInOptions object with the requestEmail option.

    If you need to request additional scopes to access Google APIs, specify them with requestScopes.

  2. Then, also in your sign-in activity's onCreate method, create a GoogleApiClient object with access to the Google Sign-In API and the options you specified.

    Note: To use enableAutoManage, your activity must extend FragmentActivity or AppCompatActivity (a subclass of FragmentActivity), both of which are part of the Android Support Library. You can use GoogleApiClient in aFragment; however, the fragment's parent activity must be a FragmentActivity. If you can't extendFragmentActivity, you must manually manage the GoogleApiClient connection lifecycle.

Add the Google Sign-In button to your app

  1. Add the SignInButton in 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" />
  2. 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 setSize and setScopes methods. Also, if you specify a Google+ social scope to setScopes, the sign-in button will be rendered with the red Google+ branding.

  3. In the Android activity (for example, in the onCreate method), register your button's OnClickListener to sign in the user when clicked:

    findViewById(R.id.sign_in_button).setOnClickListener(this);

Start the sign-in flow

  1. In the activity's onClick method, handle sign-in button taps by creating a sign-in intent with thegetSignInIntent method, and starting the intent with startActivityForResult.

    @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 beyondprofileemail, and openid, the user is also prompted to grant access to the requested resources.

  2. In the activity's onActivityResult method, retrieve the sign-in result with getSignInResultFromIntent.

    After you retrieve the sign-in result, you can check if sign-in succeeded with the isSuccess method. If sign-in succeeded, you can call the getSignInAccount method to get a GoogleSignInAccount object 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) with getId, and an ID token for the user with with getIdToken. 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的更多相关文章

  1. android app性能优化大汇总

    这里根据网络上各位大神已经总结的知识内容做一个大汇总,作为记录,方便后续“温故知新”. 性能指标: (1)使用流畅度:  图片处理器每秒刷新的帧数(FPS),可用来指示页面是否平滑的渲染.高的帧率可以 ...

  2. 谷歌发布基于机器学习的Android APP安全检测系统:Google Play Protect

    Google Play作为众所周知的在线应用市场,因为审查制度的松散,经常会有一些恶意软件伪装成其他应用混入其中.此前阿里聚安全小编就报道了2例关于恶意软件伪装在Google Play上的事件:< ...

  3. Android App在Google App Store中搜不到

    情景:Android App在Google App Store上架成功,三星手机可以在Google App Store中搜索到,但是三星tablet却无法在Google App Store中搜索到,目 ...

  4. Unity上线google商店 用IL2Cpp打包64位版本和Android APP Bundle优化 及产生的bug

    ios刚上线,这边着手改成android版本,我开始使用的是unity2017.4.1版本 上传谷歌商店是出现这两个警告: 要支持64位,但是在2017版本上没有找到64位的打包选项,猜测应该是版本的 ...

  5. 【Unity3D】Android App Bundle(aab)打包上架Google Play介绍

    总体说来,Android App Bundle打包有3种方式,每种方式都有成功上架Google Play进行测试通过,因此实用程度还是挺高的.能够理解以下内容的前提是会打apk包,知道如何生成Asse ...

  6. 第三篇:gradle 编译 Android app 概览

    引言:经过上两篇的论述,我们已经从代码到架构都简单的熟悉了一遍,理论上,只要知道android app的编译过程,我们大可以自己写一份用gradle编译app的插件,插件内将将整个流程用Task的依赖 ...

  7. MAC安裝《Genymotion Android模擬器》大玩Android APP (神魔之塔)

    链接地址:http://www.minwt.com/mac/10083.html/comment-page-2 MAC» 智慧型裝罝» Android | 2014/02/12 Android是一個開 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Codeforces Round #475 Div. 2 A B C D

    A - Splits 题意 将一个正整数拆分成若干个正整数的和,从大到小排下来,与第一个数字相同的数字的个数为这个拆分的权重. 问\(n\)的所有拆分的不同权重可能个数. 思路 全拆成1,然后每次将2 ...

  2. Pycharm2017汉化包下载链接

    https://github.com/ewen0930/PyCharm-Chinese/tree/f5a8dc4a8f34398e81a69c69bb046aa4eff27c90 1.首先下载PyCh ...

  3. [Leetcode Week4]H-Index

    H-Index题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/h-index/description/ Description Given an arr ...

  4. 【 总结 】linux中test命令详解

    test命令在bash shell脚本中经常以中括号([])的形式出现,而且在脚本中使用字母来表示比符号表示更专业,出错率更低. 测试标志 代表意义 文件名.文件类型 -e 该文件名是否存在 -f 该 ...

  5. WCF测试小程序

    using System;using System.Collections.Generic;using System.Linq;using System.Runtime.Serialization;u ...

  6. 【python】python2.x 与 python3.x区别对照+缩进错误解决方法

    仅仅列出我用到的,不全. 划重点: 1. urllib2 用 urllib.request 代替 2. urllib.urlencode 用 urllib.parse.urlencode 代替 3. ...

  7. ccf-I’m stuck!

      给定一个R行C列的地图,地图的每一个方格可能是'#', '+', '-', '|', '.', 'S', 'T'七个字符中的一个,分别表示如下意思: '#': 任何时候玩家都不能移动到此方格: ' ...

  8. 查询oracle数据库中当前数据库所有表的名称

    http://www.cnblogs.com/hyq0002013/p/5948419.html select t.table_name from user_tables t;

  9. jquery事件之select选中事件

    根据select下拉列表选中的不同选项执行不同的方法,工作中经常会用到,这里就要用到Jquery的select选中事件 这里给select加一个叫label_id的id,然后通过id选择器找到这个节点 ...

  10. web模拟telnet

    https://blog.csdn.net/midion9/article/details/51781402 https://blog.csdn.net/hackstoic/article/detai ...