Integrating Google Sign-In into your iOS app

Follow this procedure if you want your servers to be able to make Google API calls on behalf of users or while they are offline.

Before you begin

You must complete the basic Google Sign-In integration.

Enable server-side API access for your app

With the earlier Enable sign-in procedure, your app authenticates the user on the client side only; in that case, your app is able to access the Google APIs only while the user is actively using your app. With this procedure your servers can make Google API calls on behalf of users or while they are offline. For example, an photo app can enhance a photo by processing it on a backend server. To do this, your server requires an access token and a refresh token.

To obtain an access token and refresh token for your server, you can request a one-time authorization code that your server exchanges for these two tokens. You request the one-time code by specifying your server's client ID along with your other GIDSignIn parameters. After you successfully connect the user, you will find the one-time code as the auth parameter server_code accessible via the finishedWithAuth:error handler.

  1. Configure an iOS app project as described in Start Integrating.

  2. Define your app delegate's application:didFinishLaunchingWithOptions: method as described above in Enable Sign-In, but for this implementation you will set the serverClientID property as shown below.

    - (BOOL)application:(UIApplication *)application
        didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
      [GIDSignIn sharedInstance].clientID = @"APP_CLIENT_ID";
      [GIDSignIn sharedInstance].serverClientID = @"SERVER_CLIENT_ID";
    
      // Additional scopes, if any
      // [GIDSignIn sharedInstance].scopes = @[ @"other_scope" ];
    
      return YES;
    }
    
  3. After the user is signed in, retrieve the one-time authorization code:

    - (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user
        withError:(NSError *)error {
      // Perform any operations on signed in user here.
      // user.serverAuthCode now has a server authorization code!
    }
    
  4. Securely pass the serverAuthCode string to your server using HTTPS POST.

  5. On your app's backend server, exchange the auth code for access and refresh tokens. Use the access token to call Google APIs on behalf of the user and, optionally, store the refresh token to acquire a new access token when the access token expires.

    If you requested profile access, you also get an ID token that contains basic profile information for the user.

    For example:

    Java
    // (Receive authCode via HTTPS POST)
    
    // Set path to the Web application client_secret_*.json file you downloaded from the
    // Google API Console: https://console.developers.google.com/apis/credentials
    // You can also find your Web application client ID and client secret from the
    // console and specify them directly when you create the GoogleAuthorizationCodeTokenRequest
    // object.
    String CLIENT_SECRET_FILE = "/path/to/client_secret.json";
    
    // Exchange auth code for access token
    GoogleClientSecrets clientSecrets =
        GoogleClientSecrets.load(
            JacksonFactory.getDefaultInstance(), new FileReader(CLIENT_SECRET_FILE));
    GoogleTokenResponse tokenResponse =
              new GoogleAuthorizationCodeTokenRequest(
                  new NetHttpTransport(),
                  JacksonFactory.getDefaultInstance(),
                  "https://www.googleapis.com/oauth2/v4/token",
                  clientSecrets.getDetails().getClientId(),
                  clientSecrets.getDetails().getClientSecret(),
                  authCode,
                  REDIRECT_URI)  // Specify the same redirect URI that you use with your web
                                 // app. If you don't have a web version of your app, you can
                                 // specify an empty string.
                  .execute();
    
    String accessToken = tokenResponse.getAccessToken();
    
    // Use access token to call API
    GoogleCredential credential = new GoogleCredential().setAccessToken(accessToken);
    Drive drive =
        new Drive.Builder(new NetHttpTransport(), JacksonFactory.getDefaultInstance(), credential)
            .setApplicationName("Auth Code Exchange Demo")
            .build();
    File file = drive.files().get("appfolder").execute();
    
    // Get profile info from ID token
    GoogleIdToken idToken = tokenResponse.parseIdToken();
    GoogleIdToken.Payload payload = idToken.getPayload();
    String userId = payload.getSubject();  // Use this value as a key to identify a user.
    String email = payload.getEmail();
    boolean emailVerified = Boolean.valueOf(payload.getEmailVerified());
    String name = (String) payload.get("name");
    String pictureUrl = (String) payload.get("picture");
    String locale = (String) payload.get("locale");
    String familyName = (String) payload.get("family_name");
    String givenName = (String) payload.get("given_name");
    
    Python
    from apiclient import discovery
    import httplib2
    from oauth2client import client
    
    # (Receive auth_code by HTTPS POST)
    
    
    # Set path to the Web application client_secret_*.json file you downloaded from the
    # Google API Console: https://console.developers.google.com/apis/credentials
    CLIENT_SECRET_FILE = '/path/to/client_secret.json'
    
    # Exchange auth code for access token, refresh token, and ID token
    credentials = client.credentials_from_clientsecrets_and_code(
        CLIENT_SECRET_FILE,
        ['https://www.googleapis.com/auth/drive.appdata', 'profile', 'email'],
        auth_code)
    
    # Call Google API
    http_auth = credentials.authorize(httplib2.Http())
    drive_service = discovery.build('drive', 'v3', http=http_auth)
    appfolder = drive_service.files().get(fileId='appfolder').execute()
    
    # Get profile info from ID token
    userid = credentials.id_token['sub']
    email = credentials.id_token['email']