Android™ Requires Chilkat v11.5.0+
Android™
Box: Get a Service Account Token with Client Credentials
See more Box Examples
Obtain a Box service-account token using the client credentials grant. Configure and authorize a Server app first. Load credentials from protected application configuration; do not ship a client secret in a public client.
See the Box API guide for setup and the complete workflow.
Chilkat Android™ Downloads
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;
import android.app.Activity;
import com.chilkatsoft.*;
import android.widget.TextView;
import android.os.Bundle;
public class SimpleActivity extends Activity {
private static final String TAG = "Chilkat";
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean success = false;
// Requires Chilkat 11.5.0 or later and a previously unlocked Chilkat bundle.
// See Global Unlock Sample.
// Guide: Box API Guide.
success = false;
CkHttpCurl curl = new CkHttpCurl();
curl.SetVar("client_id","BOX_CLIENT_ID");
curl.SetVar("client_secret","BOX_CLIENT_SECRET");
curl.SetVar("enterprise_id","BOX_ENTERPRISE_ID");
// enterprise identifies the app's service account. This is not an admin-user token.
curl.ClearTargetOutput("*");
curl.ClearVar("access_token");
curl.AddTargetOutput("access_token","access_token");
success = curl.DoYourThing("curl -X POST \"https://api.box.com/oauth2/token\" --data-urlencode \"grant_type=client_credentials\" --data-urlencode \"client_id={{client_id}}\" --data-urlencode \"client_secret={{client_secret}}\" --data-urlencode \"box_subject_type=enterprise\" --data-urlencode \"box_subject_id={{enterprise_id}}\"");
if ((success != true) or (curl.get_StatusCode() != 200)) {
Log.i(TAG, "Box request failed. HTTP status: " + String.valueOf(curl.get_StatusCode()));
Log.i(TAG, "Failure reason: " + String.valueOf(curl.get_FailReason()));
return;
}
if (curl.VarDefined("!") != true) {
Log.i(TAG, "Expected response fields are missing.");
return;
}
// Keep this token in protected application memory; reacquire it when it expires.
// Do not print tokens or the complete token response.
Log.i(TAG, "Service-account access token obtained.");
curl.ClearTargetOutput("*");
CkStringBuilder authorizedCommand1 = new CkStringBuilder();
authorizedCommand1.SetString("curl -X GET \"https://api.box.com/2.0/users/me?fields=id,name,login\" -H \"Authorization: Bearer {{access_token}}\"");
int headerReplacement1 = authorizedCommand1.Replace("{{access_token}}",curl.getVar("access_token"));
success = curl.DoYourThing(authorizedCommand1.getAsString());
if ((success != true) or (curl.get_StatusCode() != 200)) {
Log.i(TAG, "Box request failed. HTTP status: " + String.valueOf(curl.get_StatusCode()));
Log.i(TAG, "Failure reason: " + String.valueOf(curl.get_FailReason()));
Log.i(TAG, curl.responseBodyStr());
return;
}
Log.i(TAG, curl.responseBodyStr());
}
static {
System.loadLibrary("chilkat");
// Note: If the incorrect library name is passed to System.loadLibrary,
// then you will see the following error message at application startup:
//"The application <your-application-name> has stopped unexpectedly. Please try again."
}
}