Sample code for 30+ languages & platforms
PowerBuilder Requires Chilkat v11.5.0+

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Curl
oleobject loo_AuthorizedCommand1
long li_HeaderReplacement1

li_Success = 0

//  Requires Chilkat 11.5.0 or later and a previously unlocked Chilkat bundle.
//  See Global Unlock Sample.
//  Guide: Box API Guide.

li_Success = 0

loo_Curl = create oleobject
li_rc = loo_Curl.ConnectToNewObject("Chilkat.HttpCurl")
if li_rc < 0 then
    destroy loo_Curl
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Curl.SetVar("client_id","BOX_CLIENT_ID")
loo_Curl.SetVar("client_secret","BOX_CLIENT_SECRET")
loo_Curl.SetVar("enterprise_id","BOX_ENTERPRISE_ID")
//  enterprise identifies the app's service account. This is not an admin-user token.
loo_Curl.ClearTargetOutput("*")
loo_Curl.ClearVar("access_token")
loo_Curl.AddTargetOutput("access_token","access_token")
li_Success = loo_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 (li_Success <> 1) OR (loo_Curl.StatusCode <> 200) then
    Write-Debug "Box request failed. HTTP status: " + string(loo_Curl.StatusCode)
    Write-Debug "Failure reason: " + string(loo_Curl.FailReason)
    destroy loo_Curl
    return
end if

if loo_Curl.VarDefined("!") <> 1 then
    Write-Debug "Expected response fields are missing."
    destroy loo_Curl
    return
end if

//  Keep this token in protected application memory; reacquire it when it expires.
//  Do not print tokens or the complete token response.
Write-Debug "Service-account access token obtained."
loo_Curl.ClearTargetOutput("*")
loo_AuthorizedCommand1 = create oleobject
li_rc = loo_AuthorizedCommand1.ConnectToNewObject("Chilkat.StringBuilder")

loo_AuthorizedCommand1.SetString("curl -X GET ~"https://api.box.com/2.0/users/me?fields=id,name,login~" -H ~"Authorization: Bearer {{access_token}}~"")
li_HeaderReplacement1 = loo_AuthorizedCommand1.Replace("{{access_token}}",loo_Curl.GetVar("access_token"))
li_Success = loo_Curl.DoYourThing(loo_AuthorizedCommand1.GetAsString())
if (li_Success <> 1) OR (loo_Curl.StatusCode <> 200) then
    Write-Debug "Box request failed. HTTP status: " + string(loo_Curl.StatusCode)
    Write-Debug "Failure reason: " + string(loo_Curl.FailReason)
    Write-Debug loo_Curl.ResponseBodyStr
    destroy loo_Curl
    destroy loo_AuthorizedCommand1
    return
end if

Write-Debug loo_Curl.ResponseBodyStr


destroy loo_Curl
destroy loo_AuthorizedCommand1