Integrating Box Accounts

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

Creating a Box application

To get started, you first need to create a Box application and get your Box client ID and client secret. For the detailed instructions, see the Box official documentation.

You will need to fill in the following information.

  • Application name
  • Application type (Please specify "Box Content")

When your Box application is created, you will get a Box client ID and client secret.

Next, please configure your application. In "redirect_url", please specify the value that corresponds to the server location you've selected (please put your AppID in ${your-app-id}).

  • "OAuth2 Parameters" > "redirect_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
    • 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 Box 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 "Box" to open the configuration screen for Box.

  4. Set your Box client ID and client 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 Box authentication.

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

Logging in with a Box account

Here is the sample code for letting a new user log in with their Box account:

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

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

// 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 Box 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.BOX.
  4. Authenticate the user with the logIn() method. If the specified Box 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 Box 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);
  }
}

If the login 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:

KiiSocialConnect connect = Kii.socialConnect(SocialNetwork.SOCIALNETWORK_CONNECTOR);
Bundle b = connect.getAccessTokenBundle();

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

// User id provided by the social network provider.
String providerUserId = b.getString("provider_user_id");

// If a new Kii user is created with the logIn method.
boolean kiiNewUser = b.getBoolean("kii_new_user");