DataFlex Requires Chilkat v11.5.0+
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoConfig
Variant vKey
Handle hoKey
Handle hoHeader
Handle hoJwt
Handle hoPrng
String sNonce
Handle hoClaims
String sAssertion
Handle hoCurl
String sTemp1
String sTemp2
Integer iTemp1
Boolean bTemp1
Move False To iSuccess
// Requires Chilkat 11.5.0 or later and a previously unlocked Chilkat bundle.
// See Global Unlock Sample.
// Guide: Box API Guide.
Move False To iSuccess
Get Create (RefClass(cComChilkatJsonObject)) To hoConfig
If (Not(IsComObjectCreated(hoConfig))) Begin
Send CreateComObject of hoConfig
End
Get ComLoadFile Of hoConfig "qa_data/tokens/box_jwt_config.json" To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoConfig To sTemp1
Showln sTemp1
Procedure_Return
End
Get Create (RefClass(cComChilkatPrivateKey)) To hoKey
If (Not(IsComObjectCreated(hoKey))) Begin
Send CreateComObject of hoKey
End
Get ComStringOf Of hoConfig "boxAppSettings.appAuth.privateKey" To sTemp1
Get ComStringOf Of hoConfig "boxAppSettings.appAuth.passphrase" To sTemp2
Get ComLoadEncryptedPem Of hoKey sTemp1 sTemp2 To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoKey To sTemp1
Showln sTemp1
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonObject)) To hoHeader
If (Not(IsComObjectCreated(hoHeader))) Begin
Send CreateComObject of hoHeader
End
Get ComUpdateString Of hoHeader "alg" "RS256" To iSuccess
Get ComUpdateString Of hoHeader "typ" "JWT" To iSuccess
Get ComStringOf Of hoConfig "boxAppSettings.appAuth.publicKeyID" To sTemp1
Get ComUpdateString Of hoHeader "kid" sTemp1 To iSuccess
Get Create (RefClass(cComChilkatJwt)) To hoJwt
If (Not(IsComObjectCreated(hoJwt))) Begin
Send CreateComObject of hoJwt
End
Get Create (RefClass(cComChilkatPrng)) To hoPrng
If (Not(IsComObjectCreated(hoPrng))) Begin
Send CreateComObject of hoPrng
End
Get ComGenRandom Of hoPrng 32 "hex" To sNonce
Get ComLastMethodSuccess Of hoPrng To bTemp1
If (bTemp1 <> True) Begin
Get ComLastErrorText Of hoPrng To sTemp1
Showln sTemp1
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonObject)) To hoClaims
If (Not(IsComObjectCreated(hoClaims))) Begin
Send CreateComObject of hoClaims
End
Get ComStringOf Of hoConfig "boxAppSettings.clientID" To sTemp1
Get ComUpdateString Of hoClaims "iss" sTemp1 To iSuccess
Get ComStringOf Of hoConfig "enterpriseID" To sTemp1
Get ComUpdateString Of hoClaims "sub" sTemp1 To iSuccess
Get ComUpdateString Of hoClaims "box_sub_type" "enterprise" To iSuccess
Get ComUpdateString Of hoClaims "aud" "https://api.box.com/oauth2/token" To iSuccess
Get ComUpdateString Of hoClaims "jti" sNonce To iSuccess
Get ComGenNumericDate Of hoJwt 45 To iTemp1
Get ComUpdateInt Of hoClaims "exp" iTemp1 To iSuccess
Get ComEmit Of hoHeader To sTemp1
Get ComEmit Of hoClaims To sTemp2
Get pvComObject of hoKey to vKey
Get ComCreateJwtPk Of hoJwt sTemp1 sTemp2 vKey To sAssertion
Get ComLastMethodSuccess Of hoJwt To bTemp1
If (bTemp1 <> True) Begin
Get ComLastErrorText Of hoJwt To sTemp1
Showln sTemp1
Procedure_Return
End
Get Create (RefClass(cComChilkatHttpCurl)) To hoCurl
If (Not(IsComObjectCreated(hoCurl))) Begin
Send CreateComObject of hoCurl
End
Get ComStringOf Of hoConfig "boxAppSettings.clientID" To sTemp1
Send ComSetVar To hoCurl "client_id" sTemp1
Get ComStringOf Of hoConfig "boxAppSettings.clientSecret" To sTemp1
Send ComSetVar To hoCurl "client_secret" sTemp1
Send ComSetVar To hoCurl "assertion" sAssertion
Send ComClearTargetOutput To hoCurl "*"
Send ComClearVar To hoCurl "access_token"
Send ComAddTargetOutput To hoCurl "access_token" "access_token"
Get ComDoYourThing Of hoCurl '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}}"' To iSuccess
Get ComStatusCode Of hoCurl To iTemp1
If ((iSuccess <> True) Or (iTemp1 <> 200)) Begin
Get ComStatusCode Of hoCurl To iTemp1
Showln "Box request failed. HTTP status: " iTemp1
Get ComFailReason Of hoCurl To iTemp1
Showln "Failure reason: " iTemp1
Procedure_Return
End
Get ComVarDefined Of hoCurl "!" To bTemp1
If (bTemp1 <> True) Begin
Showln "Expected response fields are missing."
Procedure_Return
End
// Reuse the token in this HttpCurl instance. JWT tokens are renewed by making
// a new assertion/token request, not by using a refresh token.
Showln "JWT access token obtained."
End_Procedure