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 need to provide the following information. In the "Callback URL", please specify the value that corresponds to the server location you've selected (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.

Then, add the following tag under the application tag in your AndroidManifest.xml so as to enable the Activity that the SDK uses for Twitter authentication.

<activity
    android:name="com.kii.cloud.storage.social.connector.KiiSocialNetworkConnectorLoginActivity"
    android:label="@string/app_name" />

Logging in with a Twitter account

There are two ways to log in with a Twitter 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 Twitter access token.

Using a Twitter access token

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

// Create a KiiSocialConnect instance.
KiiSocialConnect connect = Kii.socialConnect(KiiSocialConnect.SocialNetwork.SOCIALNETWORK_CONNECTOR);

// Specify Twitter as the social network provider for logging in to Kii Cloud.
Bundle option = new Bundle();
option.putParcelable(KiiSocialNetworkConnector.PROVIDER, KiiSocialNetworkConnector.Provider.TWITTER);

// Set a Twitter access token and a Twitter secret.
option.putString(KiiSocialNetworkConnector.ACCESS_TOKEN, accessToken);
option.putString(KiiSocialNetworkConnector.ACCESS_TOKEN_SECRET, accessTokenSecret);

// Authenticate the user to Kii Cloud with the Twitter access token and the Twitter secret.
connect.logIn(null, option, new KiiSocialCallBack() {
  @Override
  public void onLoginCompleted(KiiSocialConnect.SocialNetwork network, KiiUser user, Exception exception) {
    if (exception != null) {
      // Handle the error.
      return;
    }
  }
});

The basic steps are as follows.

  1. Get a Twitter access token and secret. For the detailed instructions, see the Twitter Kit documentation.

  2. Create an instance of a social network connector with the socialConnect() method. The argument is KiiSocialConnect.SocialNetwork.SOCIALNETWORK_CONNECTOR.

  3. Create a Bundle instance and specify the target social network. Provider.TWITTER is specified here.

  4. Set the Twitter access token and secret to the bundle.

  5. Authenticate the user with the logIn() method. If the specified account is new to Kii Cloud, the user is registered before the login. The logIn() method is non-blocking and receives the processing result with the callback method.

    When an access token is used for the login, all the processing is done in the background with no login screen for Twitter. You do not need to implement additional processings that are necessary when using the login UI provided by the SDK. Specifically, you do not need to specify the first parameter Activity of the logIn() method nor add code to the onActivityResult() 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 getCurrentUser() method. You can also retrieve the access token and related parameters with the getAccessTokenBundle() method as follows:

// Get the token bundle for the social network provider.
KiiSocialConnect connect = Kii.socialConnect(SocialNetwork.SOCIALNETWORK_CONNECTOR);
Bundle b = connect.getAccessTokenBundle();

// Get the access token.
String accessToken = b.getString("oauth_token");

// Get the secret.
String accessTokenSecret = b.getString("oauth_token_secret");

// Get the user ID provided by the social network provider.
String providerUserId = b.getString("provider_user_id");

// Check if a new Kii user was created when the logIn() method was executed.
boolean kiiNewUser = b.getBoolean("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 Twitter account:

Activity activity = this.getActivity();
KiiSocialConnect connect = Kii.socialConnect(SocialNetwork.SOCIALNETWORK_CONNECTOR);

Bundle options = new Bundle();
options.putParcelable(KiiSocialNetworkConnector.PROVIDER, KiiSocialNetworkConnector.Provider.TWITTER);

// Login.
connect.logIn(activity, options, new KiiSocialCallBack() {
  @Override
  public void onLoginCompleted(SocialNetwork network, KiiUser user, Exception exception) {
    if (exception != null) {
      // Handle the error.
      return;
    }
  }
});

The basic steps are as follows.

  1. Create an Activity for handling logging in with a Twitter account.
  2. Create a social network connector instance with the socialConnect() method. Here, we are specifying SocialNetwork.SOCIALNETWORK_CONNECTOR.
  3. Create a bundle and specify the target social network service. In this case, we are specifying Provider.TWITTER.
  4. Authenticate the user with the logIn() method. If the specified Twitter account is new to Kii Cloud, the user is registered before the login. The logIn() method is non-blocking and receives the processing result with the callback method.

Note: You also need to add a method to the onActivityResult() method to finish the Twitter authentication.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (requestCode == KiiSocialNetworkConnector.REQUEST_CODE) {
    Kii.socialConnect(SocialNetwork.SOCIALNETWORK_CONNECTOR).respondAuthOnActivityResult(
      requestCode,
      resultCode,
      data);
  }
}

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.

// Assume that a user has logged in.

// Create a KiiSocialConnect instance.
KiiSocialConnect connect = Kii.socialConnect(KiiSocialConnect.SocialNetwork.SOCIALNETWORK_CONNECTOR);

// Specify Twitter as the social network provider for logging in to Kii Cloud.
Bundle option = new Bundle();
option.putParcelable(KiiSocialNetworkConnector.PROVIDER, KiiSocialNetworkConnector.Provider.TWITTER);

// Set a Twitter access token and a Twitter secret.
option.putString(KiiSocialNetworkConnector.ACCESS_TOKEN, accessToken);
option.putString(KiiSocialNetworkConnector.ACCESS_TOKEN_SECRET, accessTokenSecret);

// Link the Twitter account with the currently logged-in Kii user.
connect.link(null, option, new KiiSocialCallBack() {
  @Override
  public void onLinkCompleted(KiiSocialConnect.SocialNetwork network, KiiUser user, Exception exception) {
    if (exception != null) {
      // Handle the error.
      return;
    }
  }
});

The basic steps are as follows.

  1. Create an instance of a social network connector with the socialConnect() method. The argument is KiiSocialConnect.SocialNetwork.SOCIALNETWORK_CONNECTOR.

  2. Create a Bundle instance and specify the target social network. Provider.TWITTER is specified here.

  3. Set a Twitter access token and secret to the bundle. For getting them, see Configuration and implementation example below. You can also see the Twitter official documentation.

  4. Link the currently logged-in user with the Twitter account with the link() method. This method is non-blocking and receives the processing result in the callback method.

Unlinking a Kii account from a Twitter account

Here is the sample code for unlinking a Kii account from a Twitter account.

// Assume that a user has logged in.

// Create a KiiSocialConnect instance.
KiiSocialConnect connect = Kii.socialConnect(KiiSocialConnect.SocialNetwork.SOCIALNETWORK_CONNECTOR);

// Specify Twitter as the social network provider for logging in to Kii Cloud.
Bundle option = new Bundle();
option.putParcelable(KiiSocialNetworkConnector.PROVIDER, KiiSocialNetworkConnector.Provider.TWITTER);

// Unlink the Twitter account from the currently logged-in Kii user.
connect.unlink(null, option, new KiiSocialCallBack() {
  @Override
  public void onUnLinkCompleted(KiiSocialConnect.SocialNetwork network, KiiUser user, Exception exception) {
    if (exception != null) {
      // Handle the error.
      return;
    }
  }
});

The basic steps are as follows.

  1. Create an instance of a social network connector with the socialConnect() method. The argument is KiiSocialConnect.SocialNetwork.SOCIALNETWORK_CONNECTOR.

  2. Create a Bundle instance and specify the target social network. Provider.TWITTER is specified here.

  3. Unlink the currently logged-in user from the Twitter account with the unlink() method. This method is non-blocking and receives the processing result in the callback method.