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

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

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Config
oleobject loo_Key
oleobject loo_Header
oleobject loo_Jwt
oleobject loo_Prng
string ls_Nonce
oleobject loo_Claims
string ls_Assertion
oleobject loo_Curl

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_Config = create oleobject
li_rc = loo_Config.ConnectToNewObject("Chilkat.JsonObject")
if li_rc < 0 then
    destroy loo_Config
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_Config.LoadFile("qa_data/tokens/box_jwt_config.json")
if li_Success <> 1 then
    Write-Debug loo_Config.LastErrorText
    destroy loo_Config
    return
end if

loo_Key = create oleobject
li_rc = loo_Key.ConnectToNewObject("Chilkat.PrivateKey")

li_Success = loo_Key.LoadEncryptedPem(loo_Config.StringOf("boxAppSettings.appAuth.privateKey"),loo_Config.StringOf("boxAppSettings.appAuth.passphrase"))
if li_Success <> 1 then
    Write-Debug loo_Key.LastErrorText
    destroy loo_Config
    destroy loo_Key
    return
end if

loo_Header = create oleobject
li_rc = loo_Header.ConnectToNewObject("Chilkat.JsonObject")

loo_Header.UpdateString("alg","RS256")
loo_Header.UpdateString("typ","JWT")
loo_Header.UpdateString("kid",loo_Config.StringOf("boxAppSettings.appAuth.publicKeyID"))
loo_Jwt = create oleobject
li_rc = loo_Jwt.ConnectToNewObject("Chilkat.Jwt")

loo_Prng = create oleobject
li_rc = loo_Prng.ConnectToNewObject("Chilkat.Prng")

ls_Nonce = loo_Prng.GenRandom(32,"hex")
if loo_Prng.LastMethodSuccess <> 1 then
    Write-Debug loo_Prng.LastErrorText
    destroy loo_Config
    destroy loo_Key
    destroy loo_Header
    destroy loo_Jwt
    destroy loo_Prng
    return
end if

loo_Claims = create oleobject
li_rc = loo_Claims.ConnectToNewObject("Chilkat.JsonObject")

loo_Claims.UpdateString("iss",loo_Config.StringOf("boxAppSettings.clientID"))
loo_Claims.UpdateString("sub",loo_Config.StringOf("enterpriseID"))
loo_Claims.UpdateString("box_sub_type","enterprise")
loo_Claims.UpdateString("aud","https://api.box.com/oauth2/token")
loo_Claims.UpdateString("jti",ls_Nonce)
loo_Claims.UpdateInt("exp",loo_Jwt.GenNumericDate(45))
ls_Assertion = loo_Jwt.CreateJwtPk(loo_Header.Emit(),loo_Claims.Emit(),loo_Key)
if loo_Jwt.LastMethodSuccess <> 1 then
    Write-Debug loo_Jwt.LastErrorText
    destroy loo_Config
    destroy loo_Key
    destroy loo_Header
    destroy loo_Jwt
    destroy loo_Prng
    destroy loo_Claims
    return
end if

loo_Curl = create oleobject
li_rc = loo_Curl.ConnectToNewObject("Chilkat.HttpCurl")

loo_Curl.SetVar("client_id",loo_Config.StringOf("boxAppSettings.clientID"))
loo_Curl.SetVar("client_secret",loo_Config.StringOf("boxAppSettings.clientSecret"))
loo_Curl.SetVar("assertion",ls_Assertion)
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=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 (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_Config
    destroy loo_Key
    destroy loo_Header
    destroy loo_Jwt
    destroy loo_Prng
    destroy loo_Claims
    destroy loo_Curl
    return
end if

if loo_Curl.VarDefined("!") <> 1 then
    Write-Debug "Expected response fields are missing."
    destroy loo_Config
    destroy loo_Key
    destroy loo_Header
    destroy loo_Jwt
    destroy loo_Prng
    destroy loo_Claims
    destroy loo_Curl
    return
end if

//  Reuse the token in this HttpCurl instance. JWT tokens are renewed by making
//  a new assertion/token request, not by using a refresh token.
Write-Debug "JWT access token obtained."


destroy loo_Config
destroy loo_Key
destroy loo_Header
destroy loo_Jwt
destroy loo_Prng
destroy loo_Claims
destroy loo_Curl