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

Visual FoxPro
LOCAL lnSuccess
LOCAL loConfig
LOCAL loKey
LOCAL loHeader
LOCAL loJwt
LOCAL loPrng
LOCAL lcNonce
LOCAL loClaims
LOCAL lcAssertion
LOCAL loCurl

lnSuccess = 0

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

lnSuccess = 0

loConfig = CreateObject('Chilkat.JsonObject')
lnSuccess = loConfig.LoadFile("qa_data/tokens/box_jwt_config.json")
IF (lnSuccess <> 1) THEN
    ? loConfig.LastErrorText
    RELEASE loConfig
    CANCEL
ENDIF

loKey = CreateObject('Chilkat.PrivateKey')
lnSuccess = loKey.LoadEncryptedPem(loConfig.StringOf("boxAppSettings.appAuth.privateKey"),loConfig.StringOf("boxAppSettings.appAuth.passphrase"))
IF (lnSuccess <> 1) THEN
    ? loKey.LastErrorText
    RELEASE loConfig
    RELEASE loKey
    CANCEL
ENDIF

loHeader = CreateObject('Chilkat.JsonObject')
loHeader.UpdateString("alg","RS256")
loHeader.UpdateString("typ","JWT")
loHeader.UpdateString("kid",loConfig.StringOf("boxAppSettings.appAuth.publicKeyID"))
loJwt = CreateObject('Chilkat.Jwt')
loPrng = CreateObject('Chilkat.Prng')
lcNonce = loPrng.GenRandom(32,"hex")
IF (loPrng.LastMethodSuccess <> 1) THEN
    ? loPrng.LastErrorText
    RELEASE loConfig
    RELEASE loKey
    RELEASE loHeader
    RELEASE loJwt
    RELEASE loPrng
    CANCEL
ENDIF

loClaims = CreateObject('Chilkat.JsonObject')
loClaims.UpdateString("iss",loConfig.StringOf("boxAppSettings.clientID"))
loClaims.UpdateString("sub",loConfig.StringOf("enterpriseID"))
loClaims.UpdateString("box_sub_type","enterprise")
loClaims.UpdateString("aud","https://api.box.com/oauth2/token")
loClaims.UpdateString("jti",lcNonce)
loClaims.UpdateInt("exp",loJwt.GenNumericDate(45))
lcAssertion = loJwt.CreateJwtPk(loHeader.Emit(),loClaims.Emit(),loKey)
IF (loJwt.LastMethodSuccess <> 1) THEN
    ? loJwt.LastErrorText
    RELEASE loConfig
    RELEASE loKey
    RELEASE loHeader
    RELEASE loJwt
    RELEASE loPrng
    RELEASE loClaims
    CANCEL
ENDIF

loCurl = CreateObject('Chilkat.HttpCurl')
loCurl.SetVar("client_id",loConfig.StringOf("boxAppSettings.clientID"))
loCurl.SetVar("client_secret",loConfig.StringOf("boxAppSettings.clientSecret"))
loCurl.SetVar("assertion",lcAssertion)
loCurl.ClearTargetOutput("*")
loCurl.ClearVar("access_token")
loCurl.AddTargetOutput("access_token","access_token")
lnSuccess = loCurl.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 ((lnSuccess <> 1) OR (loCurl.StatusCode <> 200)) THEN
    ? "Box request failed. HTTP status: " + STR(loCurl.StatusCode)
    ? "Failure reason: " + STR(loCurl.FailReason)
    RELEASE loConfig
    RELEASE loKey
    RELEASE loHeader
    RELEASE loJwt
    RELEASE loPrng
    RELEASE loClaims
    RELEASE loCurl
    CANCEL
ENDIF

IF (loCurl.VarDefined("!") <> 1) THEN
    ? "Expected response fields are missing."
    RELEASE loConfig
    RELEASE loKey
    RELEASE loHeader
    RELEASE loJwt
    RELEASE loPrng
    RELEASE loClaims
    RELEASE loCurl
    CANCEL
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.
? "JWT access token obtained."

RELEASE loConfig
RELEASE loKey
RELEASE loHeader
RELEASE loJwt
RELEASE loPrng
RELEASE loClaims
RELEASE loCurl