# New Google Sign-In API

Google Identity Services (GIS) is a new set of APIs that provides users easy and
secure sign-in and sign-up, in an easy-to-implement package for developers.
This document details a new Google sign in API (part of GIS) that can be used to
start the sign-in or sign-up flow when a user taps on a "Sign-In with Google"
button. This API can be used instead of the existing Google Sign-In APIs for
sign in flows.

> [!NOTE]
> **Note:** Google Identity Services will eventually replace the existing Google Sign-In API. For new apps we recommend using Google Identity Services instead of the Google Sign-In API for sign-in and sign-up, unless you need authorization, [Server-Side Access](https://developers.google.com/identity/sign-in/android/offline-access), or custom OAuth scopes. These features are coming in future versions of Google Identity Services.

You should use this API only when the user explicitly shows intent to sign in
with Google. For example, use this API when they click a "Sign in with Google"
button in your app.

You should not use this API to prompt the user to sign-in on app launch or in
response to another trigger such as adding an item to the shopping cart. For
these use cases, use
[One Tap sign-in and sign-up](https://developers.google.com/identity/one-tap/android/legacy-get-started).

When you start the Google Sign-In flow with the new API, it will display this
UI:

![Google Sign-In](https://developers.google.com/static/identity/sign-in/android/images/google-sign-in.png)

## Before you begin

[Configure a Google API Console project and set up your Android Studio project](https://developers.google.com/identity/sign-in/android/legacy-start-integrating).

## Make a sign-in request

To launch a Google Sign-In flow using the Identity API build a
`GetSignInRequest` object. Then, on a `SignInClient` object call
`getSignInIntent`. This call is async and on success it will provide a
`PendingIntent` to launch the dialog.

        private static final int REQUEST_CODE_GOOGLE_SIGN_IN = 1; /* unique request id */

        private void signIn() {
            GetSignInIntentRequest request =
                GetSignInIntentRequest.builder()
                    .setServerClientId(getString(R.string.server_client_id))
                    .build();

            Identity.getSignInClient(activity)
                .getSignInIntent(request)
                .addOnSuccessListener(
                        result -> {
                            try {
                                startIntentSenderForResult(
                                        result.getIntentSender(),
                                        REQUEST_CODE_GOOGLE_SIGN_IN,
                                        /* fillInIntent= */ null,
                                        /* flagsMask= */ 0,
                                        /* flagsValue= */ 0,
                                        /* extraFlags= */ 0,
                                        /* options= */ null);
                            } catch (IntentSender.SendIntentException e) {
                                Log.e(TAG, "Google Sign-in failed");
                            }
                        })
                .addOnFailureListener(
                        e -> {
                            Log.e(TAG, "Google Sign-in failed", e);
                        });
        }

## Handle sign in results

In `onActivityResult` retrieve a `SignInCredential`. The `SignInCredential`
object returned from `getSignInCredentialFromIntent` contains information
about a valid login. If the user fails to log in for some reason, an
`ApiException` is thrown.

        @Override
        public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if(resultCode == Activity.RESULT_OK) {
                if (requestCode == REQUEST_CODE_GOOGLE_SIGN_IN) {
                    try {
                        SignInCredential credential = Identity.getSignInClient(this).getSignInCredentialFromIntent(data);
                        // Signed in successfully - show authenticated UI
                        updateUI(credential);
                    } catch (ApiException e) {
                        // The ApiException status code indicates the detailed failure reason.
                    }
                }
            }
        }

> [!NOTE]
> **Note:** Consider using the [Activity Result API](https://developer.android.com/training/basics/intents/result) to manage Activity callbacks.

    private ActivityResultLauncher<IntentSenderRequest> loginResultHandler = registerForActivityResult(new ActivityResultContracts.StartIntentSenderForResult(), result -> {
        // handle intent result here
    });

The result of a successful sign in always returns the users full name, email,
and profile picture url. If you need additional information you can direct users
into a complete profile information flow.