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.

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.

Swift 3:

func myRegistrationMethod(){
  // Set a Twitter access token and a Twitter secret.
  let options : Dictionary = ["accessToken":accessToken,"accessTokenSecret":accessTokenSecret]

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

Objective-C:

- (void)myRegistrationMethod {
  // Set a Twitter access token and a Twitter secret.
  NSDictionary *options = @{@"accessToken":accessToken, @"accessTokenSecret":accessTokenSecret};

  // Authenticate the user to Kii Cloud with the Twitter access token and the Twitter secret.
  [KiiSocialConnect logIn:kiiConnectorTwitter
                  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 Twitter access token and secret. For the detailed instructions, see the Twitter Kit documentation.

  2. Set the Twitter access token and secret to the accessToken and accessTokenSecret parameters of options.

  3. Authenticate the user with the logIn:options:block: method. The first argument specifies the target social network, kiiConnectorTwitter. If the specified Twitter 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(.Twitter)! as NSDictionary

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

// Get the secret.
let accessTokenSecret = (dict.object(forKey: "oauth_token_secret") 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:kiiConnectorTwitter];

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

// Get the secret.
NSString *accessTokenSecret = [dict objectForKey:@"oauth_token_secret"];

// 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 Twitter 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: .Twitter, 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:kiiConnectorTwitter
                  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 Twitter 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 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 linkCurrentUser:options:block: method.

Note that this sample code assumes that your mobile app has obtained a Twitter access token and secret. For getting them, see Configuration and implementation example below. You can also see the Twitter official documentation.

Swift 3:

// Assume that a user has logged in.

func myAccountLinkMethod(){
  // Set a Twitter access token and a Twitter secret.
  let options : Dictionary = ["accessToken":accessToken,"accessTokenSecret":accessTokenSecret]

  // Link the Twitter account with the currently logged-in Kii user.
  KiiSocialConnect.linkCurrentUser(.Twitter, 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 Twitter access token and a Twitter secret.
  NSDictionary *options = @{
    @"access_token":accessToken,
    @"accessTokenSecret":accessTokenSecret
  }

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

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 unLinkCurrentUser:options:block: method.

Swift 3:

// Assume that a user has logged in.

func myAccountUnlinkMethod(){
  // Unlink the Twitter account from the currently logged-in Kii user.
  KiiSocialConnect.unLinkCurrentUser(.Twitter) { (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 Twitter account from the currently logged-in Kii user.
  [KiiSocialConnect unLinkCurrentUser:kiiConnectorTwitter
                                block:^(KiiUser *retUser, KiiConnectorProvider provider, NSError *retError) {
    if (retError != nil) {
      // Handle the error.
      return;
    }
  }];
}