Objective-C Requires Chilkat v11.5.0+
Objective-C
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 Objective-C Downloads
#import <CkoHttpCurl.h>
#import <CkoStringBuilder.h>
BOOL success = NO;
// Requires Chilkat 11.5.0 or later and a previously unlocked Chilkat bundle.
// See Global Unlock Sample.
// Guide: Box API Guide.
success = NO;
CkoHttpCurl *curl = [[CkoHttpCurl alloc] init];
[curl SetVar: @"client_id" varValue: @"BOX_CLIENT_ID"];
[curl SetVar: @"client_secret" varValue: @"BOX_CLIENT_SECRET"];
[curl SetVar: @"enterprise_id" varValue: @"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" varName: @"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 != YES) || ([curl.StatusCode intValue] != 200)) {
NSLog(@"%@%d",@"Box request failed. HTTP status: ",[curl.StatusCode intValue]);
NSLog(@"%@%d",@"Failure reason: ",[curl.FailReason intValue]);
return;
}
if ([curl VarDefined: @"!"] != YES) {
NSLog(@"%@",@"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.
NSLog(@"%@",@"Service-account access token obtained.");
[curl ClearTargetOutput: @"*"];
CkoStringBuilder *authorizedCommand1 = [[CkoStringBuilder alloc] init];
[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}}" replacement: [curl GetVar: @"access_token"]] intValue];
success = [curl DoYourThing: [authorizedCommand1 GetAsString]];
if ((success != YES) || ([curl.StatusCode intValue] != 200)) {
NSLog(@"%@%d",@"Box request failed. HTTP status: ",[curl.StatusCode intValue]);
NSLog(@"%@%d",@"Failure reason: ",[curl.FailReason intValue]);
NSLog(@"%@",curl.ResponseBodyStr);
return;
}
NSLog(@"%@",curl.ResponseBodyStr);