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:

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. The standard Google sign-in buttonAdd 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. Image of the Sign-In account chooserIn 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 beyondprofile, email, 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>.

posted on 2016-09-26 16:12  Sun‘刺眼的博客  阅读(1674)  评论(0编辑  收藏  举报

导航