AutoIt Requires Chilkat v11.5.0+
AutoIt
Box: Get a Service Account Token with JWT
See more Box Examples
Use the encrypted private key in a Box application configuration file to sign a short-lived assertion and exchange it for an access token. Configure and authorize a JWT Server app first. The enterprise subject obtains the service-account token.
See the Box API guide for setup and the complete workflow.
Chilkat AutoIt Downloads
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
$oConfig = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oConfig.LoadFile("qa_data/tokens/box_jwt_config.json")
If ($bSuccess <> True) Then
ConsoleWrite($oConfig.LastErrorText & @CRLF)
Exit
EndIf
$oKey = ObjCreate("Chilkat.PrivateKey")
$bSuccess = $oKey.LoadEncryptedPem($oConfig.StringOf("boxAppSettings.appAuth.privateKey"),$oConfig.StringOf("boxAppSettings.appAuth.passphrase"))
If ($bSuccess <> True) Then
ConsoleWrite($oKey.LastErrorText & @CRLF)
Exit
EndIf
$oHeader = ObjCreate("Chilkat.JsonObject")
$oHeader.UpdateString("alg","RS256")
$oHeader.UpdateString("typ","JWT")
$oHeader.UpdateString("kid",$oConfig.StringOf("boxAppSettings.appAuth.publicKeyID"))
$oJwt = ObjCreate("Chilkat.Jwt")
$oPrng = ObjCreate("Chilkat.Prng")
Local $sNonce = $oPrng.GenRandom(32,"hex")
If ($oPrng.LastMethodSuccess <> True) Then
ConsoleWrite($oPrng.LastErrorText & @CRLF)
Exit
EndIf
$oClaims = ObjCreate("Chilkat.JsonObject")
$oClaims.UpdateString("iss",$oConfig.StringOf("boxAppSettings.clientID"))
$oClaims.UpdateString("sub",$oConfig.StringOf("enterpriseID"))
$oClaims.UpdateString("box_sub_type","enterprise")
$oClaims.UpdateString("aud","https://api.box.com/oauth2/token")
$oClaims.UpdateString("jti",$sNonce)
$oClaims.UpdateInt("exp",$oJwt.GenNumericDate(45))
Local $sAssertion = $oJwt.CreateJwtPk($oHeader.Emit(),$oClaims.Emit(),$oKey)
If ($oJwt.LastMethodSuccess <> True) Then
ConsoleWrite($oJwt.LastErrorText & @CRLF)
Exit
EndIf
$oCurl = ObjCreate("Chilkat.HttpCurl")
$oCurl.SetVar "client_id",$oConfig.StringOf("boxAppSettings.clientID")
$oCurl.SetVar "client_secret",$oConfig.StringOf("boxAppSettings.clientSecret")
$oCurl.SetVar "assertion",$sAssertion
$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=urn:ietf:params:oauth:grant-type:jwt-bearer"" --data-urlencode ""client_id={{client_id}}"" --data-urlencode ""client_secret={{client_secret}}"" --data-urlencode ""assertion={{assertion}}""")
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
; Reuse the token in this HttpCurl instance. JWT tokens are renewed by making
; a new assertion/token request, not by using a refresh token.
ConsoleWrite("JWT access token obtained." & @CRLF)