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 a successful sign-in, you will find the one-time code as the serverAuthCode property of GIDGoogleUser.

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

  2. Find your server client ID on the Credentials page of the Google API Console. Your server client ID is a Web application type client named something like Web client (Auto-created for Google Sign-in).

  3. 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;
  4. 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!
  5. Securely pass the serverAuthCode string to your server using HTTPS POST.

  6. 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:

    // (Receive authCode via HTTPS POST)
    if (request.getHeader("X-Requested-With") == null) {
      // Without the `X-Requested-With` header, this request could be forged. Aborts.
    // Set path to the Web application client_secret_*.json file you downloaded from the
    // Google API Console:
    // 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 =
            JacksonFactory.getDefaultInstance(), new FileReader(CLIENT_SECRET_FILE));
    GoogleTokenResponse tokenResponse =
              new GoogleAuthorizationCodeTokenRequest(
                  new NetHttpTransport(),
                  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.
    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")
    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");
    from apiclient import discovery
    import httplib2
    from oauth2client import client
    # (Receive auth_code by HTTPS POST)
    # If this request does not have `X-Requested-With` header, this could be a CSRF
    if not request.headers.get('X-Requested-With'):
    # Set path to the Web application client_secret_*.json file you downloaded from the
    # Google API Console:
    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(
        ['', 'profile', 'email'],
    # Call Google API
    http_auth = credentials.authorize(httplib2.Http())
    drive_service ='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']