Integrating Twitter Accounts

This topic explains how to allow your users to sign up for and log in to your mobile app with their Twitter 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 Twitter Integration

Creating a Twitter application

To get started, you first need to create a Twitter application and get your Twitter consumer key and consumer secret. For the detailed instructions, see the Twitter official documentation.

You will need to fill in the following fields (please put your AppID in ${your-app-id}):

  • Name
  • Description
  • Website
  • Callback URL
    • 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 your Twitter application is created, you will get a Twitter consumer key and consumer secret.

Configuring a Kii application

Take the following steps in the developer portal to configure your application by setting your Twitter consumer key and consumer secret.

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

  2. Click "SOCIAL NETWORKS".

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

  4. Set your Twitter consumer key and consumer secret.

Logging in with a Twitter account

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

  • // Set a Twitter access token and a Twitter secret.
    var options = {
      oauth_token: __obtained_access_token__,
      oauth_token_secret: __obtained_access_token_secret__
    };
    
    // Authenticate the user to Kii Cloud with the Twitter access token and the Twitter secret.
    KiiSocialConnect.logIn(KiiSocialNetworkName.TWITTER, options, {
      success: function(user, network) {
        // Do something.
      },
      failure: function(user, network, errorString) {
        // Handle the error.
      }
    });
  • // Set a Twitter access token and a Twitter secret.
    var options = {
      oauth_token: __obtained_access_token__,
      oauth_token_secret: __obtained_access_token_secret__
    };
    
    // Authenticate the user to Kii Cloud with the Twitter access token and the Twitter secret.
    KiiSocialConnect.logIn(KiiSocialNetworkName.TWITTER, 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 Twitter access token and secret. For the detailed instructions, see the Twitter official documentation.

  2. Set the Twitter access token and secret to the oauth_token and oauth_token_secret parameters of options.

  3. Authenticate the user with the logIn() method. The first argument specifies the target social network, KiiSocialNetworkName.TWITTER. If the specified Twitter 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 Twitter account

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

Here is the sample code for linking a Kii account to a Twitter account. You basically just need to execute the linkCurrentUserWithNetwork() method.

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

  • // Assume that a user has logged in.
    
    // Set a Twitter access token and a Twitter secret.
    var options = {
      oauth_token: __obtained_access_token__,
      oauth_token_secret: __obtained_access_token_secret__
    };
    
    // Link the Twitter account with the currently logged-in Kii user.
    KiiSocialConnect.linkCurrentUserWithNetwork(KiiSocialNetworkName.TWITTER, options, {
      success: function(user, network) {
        // Do something.
      },
      failure: function(user, network, errorString) {
        // Handle the error.
      }
    });
  • // Assume that a user has logged in.
    
    // Set a Twitter access token and a Twitter secret.
    var options = {
      oauth_token: __obtained_access_token__,
      oauth_token_secret: __obtained_access_token_secret__
    };
    
    // Link the Twitter account with the currently logged-in Kii user.
    KiiSocialConnect.linkCurrentUserWithNetwork(KiiSocialNetworkName.TWITTER, 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 Twitter account

Here is the sample code for unlinking a Kii account from a Twitter account. You basically just need to execute the unLinkCurrentUserFromNetwork() method.

  • // Assume that a user has logged in.
    
    // Unlink the Twitter account from the currently logged-in Kii user.
    KiiSocialConnect.unLinkCurrentUserFromNetwork(KiiSocialNetworkName.TWITTER, {
      success: function(user, network) {
        // Do something.
      },
      failure: function(user, network, errorString) {
        // Handle the error.
      }
    });
  • // Assume that a user has logged in.
    
    // Unlink the Twitter account from the currently logged-in Kii user.
    KiiSocialConnect.unLinkCurrentUserFromNetwork(KiiSocialNetworkName.TWITTER).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;
      }
    );