Sample code for 30+ languages & platforms
AutoIt 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 AutoIt Downloads

AutoIt
Local $bSuccess = False

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

$bSuccess = False

$oCurl = ObjCreate("Chilkat.HttpCurl")
$oCurl.SetVar "client_id","BOX_CLIENT_ID"
$oCurl.SetVar "client_secret","BOX_CLIENT_SECRET"
$oCurl.SetVar "enterprise_id","BOX_ENTERPRISE_ID"
;  enterprise identifies the app's service account. This is not an admin-user token.
$oCurl.ClearTargetOutput "*"
$oCurl.ClearVar "access_token"
$oCurl.AddTargetOutput "access_token","access_token"
$bSuccess = $oCurl.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 (($bSuccess <> True) Or ($oCurl.StatusCode <> 200)) Then
    ConsoleWrite("Box request failed. HTTP status: " & $oCurl.StatusCode & @CRLF)
    ConsoleWrite("Failure reason: " & $oCurl.FailReason & @CRLF)
    Exit
EndIf

If ($oCurl.VarDefined("!") <> True) Then
    ConsoleWrite("Expected response fields are missing." & @CRLF)
    Exit
EndIf

;  Keep this token in protected application memory; reacquire it when it expires.
;  Do not print tokens or the complete token response.
ConsoleWrite("Service-account access token obtained." & @CRLF)
$oCurl.ClearTargetOutput "*"
$oAuthorizedCommand1 = ObjCreate("Chilkat.StringBuilder")
$oAuthorizedCommand1.SetString("curl -X GET ""https://api.box.com/2.0/users/me?fields=id,name,login"" -H ""Authorization: Bearer {{access_token}}""")
Local $iHeaderReplacement1 = $oAuthorizedCommand1.Replace("{{access_token}}",$oCurl.GetVar("access_token"))
$bSuccess = $oCurl.DoYourThing($oAuthorizedCommand1.GetAsString())
If (($bSuccess <> True) Or ($oCurl.StatusCode <> 200)) Then
    ConsoleWrite("Box request failed. HTTP status: " & $oCurl.StatusCode & @CRLF)
    ConsoleWrite("Failure reason: " & $oCurl.FailReason & @CRLF)
    ConsoleWrite($oCurl.ResponseBodyStr & @CRLF)
    Exit
EndIf

ConsoleWrite($oCurl.ResponseBodyStr & @CRLF)