PureBasic Requires Chilkat v11.5.0+
PureBasic
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 PureBasic Downloads
IncludeFile "CkJwt.pb"
IncludeFile "CkHttpCurl.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkPrng.pb"
IncludeFile "CkPrivateKey.pb"
Procedure ChilkatExample()
success.i = 0
; Requires Chilkat 11.5.0 or later and a previously unlocked Chilkat bundle.
; See Global Unlock Sample.
; Guide: Box API Guide.
success = 0
config.i = CkJsonObject::ckCreate()
If config.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkJsonObject::ckLoadFile(config,"qa_data/tokens/box_jwt_config.json")
If success <> 1
Debug CkJsonObject::ckLastErrorText(config)
CkJsonObject::ckDispose(config)
ProcedureReturn
EndIf
key.i = CkPrivateKey::ckCreate()
If key.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPrivateKey::ckLoadEncryptedPem(key,CkJsonObject::ckStringOf(config,"boxAppSettings.appAuth.privateKey"),CkJsonObject::ckStringOf(config,"boxAppSettings.appAuth.passphrase"))
If success <> 1
Debug CkPrivateKey::ckLastErrorText(key)
CkJsonObject::ckDispose(config)
CkPrivateKey::ckDispose(key)
ProcedureReturn
EndIf
header.i = CkJsonObject::ckCreate()
If header.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(header,"alg","RS256")
CkJsonObject::ckUpdateString(header,"typ","JWT")
CkJsonObject::ckUpdateString(header,"kid",CkJsonObject::ckStringOf(config,"boxAppSettings.appAuth.publicKeyID"))
jwt.i = CkJwt::ckCreate()
If jwt.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
prng.i = CkPrng::ckCreate()
If prng.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
nonce.s = CkPrng::ckGenRandom(prng,32,"hex")
If CkPrng::ckLastMethodSuccess(prng) <> 1
Debug CkPrng::ckLastErrorText(prng)
CkJsonObject::ckDispose(config)
CkPrivateKey::ckDispose(key)
CkJsonObject::ckDispose(header)
CkJwt::ckDispose(jwt)
CkPrng::ckDispose(prng)
ProcedureReturn
EndIf
claims.i = CkJsonObject::ckCreate()
If claims.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(claims,"iss",CkJsonObject::ckStringOf(config,"boxAppSettings.clientID"))
CkJsonObject::ckUpdateString(claims,"sub",CkJsonObject::ckStringOf(config,"enterpriseID"))
CkJsonObject::ckUpdateString(claims,"box_sub_type","enterprise")
CkJsonObject::ckUpdateString(claims,"aud","https://api.box.com/oauth2/token")
CkJsonObject::ckUpdateString(claims,"jti",nonce)
CkJsonObject::ckUpdateInt(claims,"exp",CkJwt::ckGenNumericDate(jwt,45))
assertion.s = CkJwt::ckCreateJwtPk(jwt,CkJsonObject::ckEmit(header),CkJsonObject::ckEmit(claims),key)
If CkJwt::ckLastMethodSuccess(jwt) <> 1
Debug CkJwt::ckLastErrorText(jwt)
CkJsonObject::ckDispose(config)
CkPrivateKey::ckDispose(key)
CkJsonObject::ckDispose(header)
CkJwt::ckDispose(jwt)
CkPrng::ckDispose(prng)
CkJsonObject::ckDispose(claims)
ProcedureReturn
EndIf
curl.i = CkHttpCurl::ckCreate()
If curl.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkHttpCurl::ckSetVar(curl,"client_id",CkJsonObject::ckStringOf(config,"boxAppSettings.clientID"))
CkHttpCurl::ckSetVar(curl,"client_secret",CkJsonObject::ckStringOf(config,"boxAppSettings.clientSecret"))
CkHttpCurl::ckSetVar(curl,"assertion",assertion)
CkHttpCurl::ckClearTargetOutput(curl,"*")
CkHttpCurl::ckClearVar(curl,"access_token")
CkHttpCurl::ckAddTargetOutput(curl,"access_token","access_token")
success = CkHttpCurl::ckDoYourThing(curl,"curl -X POST " + Chr(34) + "https://api.box.com/oauth2/token" + Chr(34) + " --data-urlencode " + Chr(34) + "grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer" + Chr(34) + " --data-urlencode " + Chr(34) + "client_id={{client_id}}" + Chr(34) + " --data-urlencode " + Chr(34) + "client_secret={{client_secret}}" + Chr(34) + " --data-urlencode " + Chr(34) + "assertion={{assertion}}" + Chr(34))
If (success <> 1) OR (CkHttpCurl::ckStatusCode(curl) <> 200)
Debug "Box request failed. HTTP status: " + Str(CkHttpCurl::ckStatusCode(curl))
Debug "Failure reason: " + Str(CkHttpCurl::ckFailReason(curl))
CkJsonObject::ckDispose(config)
CkPrivateKey::ckDispose(key)
CkJsonObject::ckDispose(header)
CkJwt::ckDispose(jwt)
CkPrng::ckDispose(prng)
CkJsonObject::ckDispose(claims)
CkHttpCurl::ckDispose(curl)
ProcedureReturn
EndIf
If CkHttpCurl::ckVarDefined(curl,"!") <> 1
Debug "Expected response fields are missing."
CkJsonObject::ckDispose(config)
CkPrivateKey::ckDispose(key)
CkJsonObject::ckDispose(header)
CkJwt::ckDispose(jwt)
CkPrng::ckDispose(prng)
CkJsonObject::ckDispose(claims)
CkHttpCurl::ckDispose(curl)
ProcedureReturn
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.
Debug "JWT access token obtained."
CkJsonObject::ckDispose(config)
CkPrivateKey::ckDispose(key)
CkJsonObject::ckDispose(header)
CkJwt::ckDispose(jwt)
CkPrng::ckDispose(prng)
CkJsonObject::ckDispose(claims)
CkHttpCurl::ckDispose(curl)
ProcedureReturn
EndProcedure