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

Box: Refresh and Save Rotated OAuth Tokens

See more Box Examples

Refresh the user token saved by the authorization example. Box refresh tokens are single-use and expire after 60 days. Serialize refreshes for a token pair and save the replacement immediately; do not run concurrent refresh jobs.

See the Box API guide for setup and the complete workflow.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oTokens
LOCAL oOauth2

nSuccess := 0

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

nSuccess := 0

oTokens := CreateObject("Chilkat.JsonObject")
nSuccess := oTokens:LoadFile("qa_data/tokens/box_oauth.json")
IF (nSuccess != 1)
    ? oTokens:LastErrorText
    oTokens:destroy()
    RETURN
ENDIF

oOauth2 := CreateObject("Chilkat.OAuth2")
oOauth2:TokenEndpoint := "https://api.box.com/oauth2/token"
oOauth2:ClientId := "BOX_CLIENT_ID"
oOauth2:ClientSecret := "BOX_CLIENT_SECRET"
oOauth2:RefreshToken := oTokens:StringOf("refresh_token")
nSuccess := oOauth2:RefreshAccessToken()
IF (nSuccess != 1)
    ? oOauth2:LastErrorText
    oTokens:destroy()
    oOauth2:destroy()
    RETURN
ENDIF

nSuccess := oTokens:Load(oOauth2:AccessTokenResponse)
IF (nSuccess != 1)
    ? oTokens:LastErrorText
    oTokens:destroy()
    oOauth2:destroy()
    RETURN
ENDIF

//  The previous refresh token is now invalid. Preserve this response even if
//  your normal token-store write fails. Do not retry with the old refresh token.
nSuccess := oTokens:WriteFile("qa_data/tokens/box_oauth.json")
IF (nSuccess != 1)
    ? oTokens:LastErrorText
    oTokens:destroy()
    oOauth2:destroy()
    RETURN
ENDIF

? "New access and refresh tokens saved. Do not log their values."

oTokens:destroy()
oOauth2:destroy()