This page refers to a deprecated version of Google Sign-In. New projects should use the current version.

Enabling Server-Side Access

With the earlier Add Sign-In procedure, your app uses GoogleApiClient.connect() to authenticate the user on the client side only; in that case, GoogleApiClient is able to access the Google APIs only while the user is actively using your app. If you want your servers to be able to make Google API calls on behalf of users or while they are offline, your server requires an access token and a refresh token.

Migrate to the current API

The GoogleApiClient.Builder.requestServerAuthCode method and ServerAuthCodeCallbacks interface were deprecated in Play Services 8.3 and removed in Play Services 8.4. To access Google APIs on behalf of users from your app's backend, you must use the current method instead.

To migrate to the current API:

  1. Configure the server auth code flow with GoogleSignInOptions.Builder.requestServerAuthCode instead of GoogleApiClient.Builder.requestServerAuthCode. For example, change this:

    /*
     * Old version
     */
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Plus.API)  // No longer needed for sign-in
            .addScope(new Scope(Scopes.DRIVE_APPFOLDER))
            .requestServerAuthCode(SERVER_CLIENT_ID, this)
            .build();
    

    To this:

    /*
     * New version
     */
    GoogleSignInOptions gso =
            new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                    .requestEmail()
                    .requestScopes(new Scope(Scopes.DRIVE_APPFOLDER))
                    .requestServerAuthCode(SERVER_CLIENT_ID, false)
                    .enableAutoManage(this, null)
                    .build();
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();
    
  2. Instead of sending the server auth code to your backend in the onUploadServerAuthCode callback, do so in the onActivityResult callback when the sign-in intent completes. Unlike onUploadServerAuthCode, onActivityResult runs in the UI thread, so you must send the auth code using Volley or AsyncTask. For example, change this:

    /*
     * Old version
     */
    @Override
    public boolean onUploadServerAuthCode(String idToken, String serverAuthCode) {
        // Send serverAuthCode to server via HTTPS POST.
    }
    

    To this:

    /*
     * New version
     */
    
    // Call when user clicks the sign-in button
    private void signIn() {
        Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
        startActivityForResult(signInIntent, RC_SIGN_IN);
    }
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    
        // Result returned from launching the Intent from
        // GoogleSignInApi.getSignInIntent(...);
        if (requestCode == RC_SIGN_IN) {
            GoogleSignInResult result =
                    Auth.GoogleSignInApi.getSignInResultFromIntent(data);
           if (result.isSuccess()) {
               GoogleSignInAccount signInAcount = result.getSignInAccount();
               String serverAuthCode = signInAccount.getServerAuthCode();
               // Send serverAuthCode to server via HTTPS POST using Volley or AsyncTask.
           }
        }
    }
    
  3. If you used the code examples from the legacy documentation, remove the mIsResolving and mShouldResolve variables and the related code from onConnectionFailed. Also, remove the call to mGoogleApiClient.connect from onActivityResult.