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

The following example illustrates how to authenticate a user with their Google account:

  • // Set a Google access token.
    var options = {
      access_token: __obtained_access_token__
    };
    
    // Authenticate the user to Kii Cloud with the Google access token.
    KiiSocialConnect.logIn(KiiSocialNetworkName.GOOGLEPLUS, options, {
      success: function(user, network) {
        // Do something.
      },
      failure: function(user, network, errorString) {
        // Handle the error.
      }
    });
  • // Set a Google access token.
    var options = {
      access_token: __obtained_access_token__
    };
    
    // Authenticate the user to Kii Cloud with the Google access token.
    KiiSocialConnect.logIn(KiiSocialNetworkName.GOOGLEPLUS, options).then(
      function(params) {
        var user = params[0];
        var network = params[1];
        // Do something.
      }
    ).catch(
      function(error) {
        // Handle the error.
        var errorString = error.message;
        var network = error.network;
      }
    );

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 access_token parameter of options.

  3. Authenticate the user with the logIn() method. The first argument specifies the target social network, KiiSocialNetworkName.GOOGLEPLUS. If the specified Google account is new to Kii Cloud, the user is registered before the login.

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 KiiUser.getCurrentUser() 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 linkCurrentUserWithNetwork() 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.

  • // Assume that a user has logged in.
    
    // Set a Google access token.
    var options = {
      access_token: __obtained_access_token__
    };
    
    // Link the Google account with the currently logged-in Kii user.
    KiiSocialConnect.linkCurrentUserWithNetwork(KiiSocialNetworkName.GOOGLEPLUS, options, {
      success: function(user, network) {
        // Do something.
      },
      failure: function(user, network, errorString) {
        // Handle the error.
      }
    });
  • // Assume that a user has logged in.
    
    // Set a Google access token.
    var options = {
      access_token: __obtained_access_token__
    };
    
    // Link the Google account with the currently logged-in Kii user.
    KiiSocialConnect.linkCurrentUserWithNetwork(KiiSocialNetworkName.GOOGLEPLUS, options).then(
      function(params) {
        var user = params[0];
        var network = params[1];
        // Do something.
      }
    ).catch(
      function(error) {
        // Handle the error.
        var user = error.target;
        var errorString = error.message;
        var network = error.network;
      }
    );

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 unLinkCurrentUserFromNetwork() method.

  • // Assume that a user has logged in.
    
    // Unlink the Google account from the currently logged-in Kii user.
    KiiSocialConnect.unLinkCurrentUserFromNetwork(KiiSocialNetworkName.GOOGLEPLUS, {
      success: function(user, network) {
        // Do something.
      },
      failure: function(user, network, errorString) {
        // Handle the error.
      }
    });
  • // Assume that a user has logged in.
    
    // Unlink the Google account from the currently logged-in Kii user.
    KiiSocialConnect.unLinkCurrentUserFromNetwork(KiiSocialNetworkName.GOOGLEPLUS).then(
      function(params) {
        var user = params[0];
        var network = params[1];
        // Do something.
      }
    ).catch(
      function(error) {
        // Handle the error.
        var user = error.target;
        var errorString = error.message;
        var network = error.network;
      }
    );