Visual FoxPro Requires Chilkat v11.5.0+
Visual FoxPro
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 Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loCurl
LOCAL loAuthorizedCommand1
LOCAL lnHeaderReplacement1
lnSuccess = 0
* Requires Chilkat 11.5.0 or later and a previously unlocked Chilkat bundle.
* See Global Unlock Sample.
* Guide: Box API Guide.
lnSuccess = 0
loCurl = CreateObject('Chilkat.HttpCurl')
loCurl.SetVar("client_id","BOX_CLIENT_ID")
loCurl.SetVar("client_secret","BOX_CLIENT_SECRET")
loCurl.SetVar("enterprise_id","BOX_ENTERPRISE_ID")
* enterprise identifies the app's service account. This is not an admin-user token.
loCurl.ClearTargetOutput("*")
loCurl.ClearVar("access_token")
loCurl.AddTargetOutput("access_token","access_token")
lnSuccess = loCurl.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 ((lnSuccess <> 1) OR (loCurl.StatusCode <> 200)) THEN
? "Box request failed. HTTP status: " + STR(loCurl.StatusCode)
? "Failure reason: " + STR(loCurl.FailReason)
RELEASE loCurl
CANCEL
ENDIF
IF (loCurl.VarDefined("!") <> 1) THEN
? "Expected response fields are missing."
RELEASE loCurl
CANCEL
ENDIF
* Keep this token in protected application memory; reacquire it when it expires.
* Do not print tokens or the complete token response.
? "Service-account access token obtained."
loCurl.ClearTargetOutput("*")
loAuthorizedCommand1 = CreateObject('Chilkat.StringBuilder')
loAuthorizedCommand1.SetString('curl -X GET "https://api.box.com/2.0/users/me?fields=id,name,login" -H "Authorization: Bearer {{access_token}}"')
lnHeaderReplacement1 = loAuthorizedCommand1.Replace("{{access_token}}",loCurl.GetVar("access_token"))
lnSuccess = loCurl.DoYourThing(loAuthorizedCommand1.GetAsString())
IF ((lnSuccess <> 1) OR (loCurl.StatusCode <> 200)) THEN
? "Box request failed. HTTP status: " + STR(loCurl.StatusCode)
? "Failure reason: " + STR(loCurl.FailReason)
? loCurl.ResponseBodyStr
RELEASE loCurl
RELEASE loAuthorizedCommand1
CANCEL
ENDIF
? loCurl.ResponseBodyStr
RELEASE loCurl
RELEASE loAuthorizedCommand1