Integrating Sina Weibo (新浪微博) Accounts

This topic explains how to allow your users to sign up for and log in to your mobile app with their Sina Weibo (新浪微博) 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 Sina Weibo integration

Creating a Sina Weibo application

To get started, you first need to create a Sina Weibo application and get your Sina Weibo (新浪微博) app key and app secret. For the detailed instructions, see the Sina Weibo (新浪微博) official documentation.

You will need to fill in the following information.

  • Application name
  • Application URL

When your Sina Weibo application is created, you will get a Sina Weibo (新浪微博) app key and app secret.

Next, please configure your application. In "授权回调页" and "取消授权回调页", please specify the value that corresponds to the server location you've selected (please put your AppID in ${your-app-id}).

  • "应用信息" > "高级信息" > "OAuth2.0 授权设置"
    • "授权回调页"
    • "取消授权回调页"
      • 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
      • China: https://${your-app-id}.cn3.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

Configuring a Kii application

Take the following steps in the developer portal to configure your application by setting your Sina Weibo (新浪微博) app key and app secret.

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

  2. Click "SOCIAL NETWORKS".

  3. Click "Sina (Weibo)" to open the configuration screen for Sina Weibo.

  4. Set your Sina Weibo (新浪微博) app key and app secret.

Logging in with a Sina Weibo account

Here is the sample code for letting a new user log in with their Sina Weibo 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: .Sina, 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:kiiConnectorSina
                  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 Sina Weibo 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:

let dict  = KiiSocialConnect.accessTokenDictionary(.Sina)! as NSDictionary

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

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

// If a new Kii user is created with the logIn method.
let kiiNewUser : Bool = (dict.object(forKey: "kii_new_user") as? NSNumber)!.boolValue

Objective-C:

NSDictionary *dict = [KiiSocialConnect accessTokenDictionary:kiiConnectorSina];

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

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

// If a new Kii user is created with the logIn method.
BOOL kiiNewUser = [dict valueForKey:@"kii_new_user"];