Integrating Google Accounts

This topic explains how to allow your users to sign up for and log in to your mobile app with their Google account. Just add a few parameters in the developer portal and a few lines of code in your mobile app and you will be social-ready!

Configuring Google integration

Creating a Google application

To get started, you first need to create a Google application and get your Google client ID and client secret. For the detailed instructions, see the Google official documentation.

After you create a Google application, follow the steps below to get your Google client ID and client secret. In the "Authorized JavaScript origins" and "Authorized redirect URI", please specify the value that corresponds to the server location you've selected (please put your AppID in ${your-app-id}).

  1. Select "APIs & Auth" > "APIs" and enable "Google+ API".
  2. Select "APIs & Auth" > "Credentials" and set the following information on "OAuth" > "Create new client ID".
    • Application Type (Please specify "Web application" here)
    • Authorized JavaScript origins
      • United States: https://${your-app-id}.us.kiiapps.com
      • Japan: https://${your-app-id}.jp.kiiapps.com
      • Singapore: https://${your-app-id}.sg.kiiapps.com
      • Europe: https://${your-app-id}.eu.kiiapps.com
    • Authorized redirect URI
      • United States: https://${your-app-id}.us.kiiapps.com/api/apps/${your-app-id}/integration/webauth/callback
      • Japan: https://${your-app-id}.jp.kiiapps.com/api/apps/${your-app-id}/integration/webauth/callback
      • Singapore: https://${your-app-id}.sg.kiiapps.com/api/apps/${your-app-id}/integration/webauth/callback
      • Europe: https://${your-app-id}.eu.kiiapps.com/api/apps/${your-app-id}/integration/webauth/callback

When the setting is done, you will get a Google client ID and client secret.

Configuring a Kii application

Take the following steps in the developer portal to configure your application by setting your Google client ID and client secret.

  1. Click on the "Edit" button in your application console in the developer portal.

  2. Click "SOCIAL NETWORKS".

  3. Click "Google" to open the configuration screen for Google.

  4. Set your Google client ID and client secret.

Logging in with a Google account

There are two ways to log in with a Google account.

The latter method (using the login UI) will be discontinued in near future. It is now outside of our support. Use the former method of using a Google access token.

Using a Google access token

The following example illustrates how to authenticate a user by using a Google access token.

Swift 3:

func myRegistrationMethod(){
  // Set a Google access token.
  let options : Dictionary = ["accessToken":accessToken]

  // Authenticate the user to Kii Cloud with the Google access token.
  KiiSocialConnect.log(in: .Googleplus, options: options) { (retUser :KiiUser?, provider : KiiConnectorProvider, retError : Error? ) -> Void in
    if (retError != nil) {
      // Handle the error.
      return
    }
  }
}

Objective-C:

- (void)myRegistrationMethod {
  // Set a Google access token.
  NSDictionary *options = @{@"accessToken" : accessToken};

  // Authenticate the user to Kii Cloud with the Google access token.
  [KiiSocialConnect logIn:kiiConnectorGooglePlus
                  options:options
                    block:^(KiiUser *retUser, KiiConnectorProvider provider, NSError *retError) {
    if (retError != nil) {
      // Handle the error.
      return;
    }
  }];
}

The basic steps are as follows.

  1. Get a Google access token. For the detailed instructions, see the Google official documentation.

  2. Set the Google access token to the accessToken parameter of options.

  3. Authenticate the user with the logIn:options:block: method. The first argument specifies the target social network, kiiConnectorGooglePlus. If the specified Google account is new to Kii Cloud, the user is registered before the login. The logIn:options:block: method is non-blocking and receives the processing result with the callback method.

If the login is successful, the user is cached inside the SDK as the current user and you can get the user information with the currentUser method. You can also retrieve the access token and related parameters with the accessTokenDictionary: method as follows:

Swift 3:

// Get the token dictionary for the social network provider.
let dict  = KiiSocialConnect.accessTokenDictionary(.Googleplus)! as NSDictionary

// Get the access token.
let accessToken = (dict.object(forKey: "oauth_token") as? String)!

// Get the user ID provided by the social network provider.
let providerUserId = (dict.object(forKey: "provider_user_id") as? String)!

// Check if a new Kii user was created when the log(in:options:_:) method was executed.
let kiiNewUser : Bool = (dict.object(forKey: "kii_new_user") as? NSNumber)!.boolValue

Objective-C:

// Get the token dictionary for the social network provider.
NSDictionary *dict = [KiiSocialConnect accessTokenDictionary:kiiConnectorGooglePlus];

// Get the access token.
NSString *accessToken = [dict objectForKey:@"oauth_token"];

// Get the user ID provided by the social network provider.
NSString *providerUserId = [dict objectForKey:@"provider_user_id"];

// Check if a new Kii user was created when the logIn:options:block: method was executed.
BOOL kiiNewUser = [dict valueForKey:@"kii_new_user"];

Using the login UI provided by the SDK (Deprecated)

Here is the sample code for letting a new user log in with their Google account:

Swift 3:

func myRegistrationMethod(){
  // Set options to nil to indicate that SDK will handle the UI
  let options : Dictionary<String,AnyObject>? = nil

  //Login

  KiiSocialConnect.log(in: .Googleplus, options: options) { (retUser :KiiUser?, provider : KiiConnectorProvider, retError : Error? ) -> Void in
    if (retError != nil) {
      // Handle the error.
      return
    }
    // The user has logged in successfully
  }
}

Objective-C:

- (void)myRegistrationMethod {
  // Set options to nil to indicate that SDK will handle the UI
  NSDictionary *options = nil;

  // Login.
  [KiiSocialConnect logIn:kiiConnectorGooglePlus
                  options:options
                    block:^(KiiUser *retUser, KiiConnectorProvider provider, NSError *retError) {
    if (retError != nil) {
      // Handle the error.
      return;
    }
    // The user has logged in successfully
  }];
}

The basic steps are as follows.

  1. Set nil to options to specify the authentication mechanism.
  2. Authenticate the user with the logIn:options:block: method. If the specified Google account is new to Kii Cloud, the user is registered before the login. The logIn:options:block: method is non-blocking and receives the processing result with the callback method.

Linking a Kii account to a Google account

You can link existing Kii users to their Google accounts. Linked users can log in to Kii Cloud via the Google authentication.

Here is the sample code for linking a Kii account to a Google account. You basically just need to execute the linkCurrentUser:options:block: method.

Note that this sample code assumes that your mobile app has obtained a Google access token. For getting a Google access token, see the Google official documentation.

Swift 3:

// Assume that a user has logged in.

func myAccountLinkMethod(){
  // Set a Google access token.
  let options : Dictionary = ["accessToken":accessToken]

  // Link the Google account with the currently logged-in Kii user.
  KiiSocialConnect.linkCurrentUser(.Googleplus, options: options) { (retUser :KiiUser?, provider : KiiConnectorProvider, retError : Error? ) -> Void in
    if (retError != nil) {
      // Handle the error.
      return
    }
  }
}

Objective-C:

// Assume that a user has logged in.

- (void) myAccountLinkMethod {
  // Set a Google access token.
  NSDictionary *options = @{
    @"access_token":accessToken
  }

  // Link the Google account with the currently logged-in Kii user.
  [KiiSocialConnect linkCurrentUser:kiiConnectorGooglePlus
                            options:options
                              block:^(KiiUser *retUser, KiiConnectorProvider provider, NSError *retError) {
    if (retError != nil) {
      // Handle the error.
      return;
    }
  }];
}

Unlinking a Kii account from a Google account

Here is the sample code for unlinking a Kii account from a Google account. You basically just need to execute the unLinkCurrentUser:options:block: method.

Swift 3:

// Assume that a user has logged in.

func myAccountUnlinkMethod(){
  // Unlink the Google account from the currently logged-in Kii user.
  KiiSocialConnect.unLinkCurrentUser(.Googleplus) { (retUser :KiiUser?, provider : KiiConnectorProvider, retError : Error? ) -> Void in
    if (retError != nil) {
      // Handle the error.
      return
    }
  }
}

Objective-C:

// Assume that a user has logged in.

- (void) myAccountUnlinkMethod {
  // Unlink the Google account from the currently logged-in Kii user.
  [KiiSocialConnect unLinkCurrentUser:kiiConnectorGooglePlus
                                block:^(KiiUser *retUser, KiiConnectorProvider provider, NSError *retError) {
    if (retError != nil) {
      // Handle the error.
      return;
    }
  }];
}