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

Visual FoxPro
LOCAL lnSuccess
LOCAL loTokens
LOCAL loOauth2

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

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

loOauth2 = CreateObject('Chilkat.OAuth2')
loOauth2.TokenEndpoint = "https://api.box.com/oauth2/token"
loOauth2.ClientId = "BOX_CLIENT_ID"
loOauth2.ClientSecret = "BOX_CLIENT_SECRET"
loOauth2.RefreshToken = loTokens.StringOf("refresh_token")
lnSuccess = loOauth2.RefreshAccessToken()
IF (lnSuccess <> 1) THEN
    ? loOauth2.LastErrorText
    RELEASE loTokens
    RELEASE loOauth2
    CANCEL
ENDIF

lnSuccess = loTokens.Load(loOauth2.AccessTokenResponse)
IF (lnSuccess <> 1) THEN
    ? loTokens.LastErrorText
    RELEASE loTokens
    RELEASE loOauth2
    CANCEL
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.
lnSuccess = loTokens.WriteFile("qa_data/tokens/box_oauth.json")
IF (lnSuccess <> 1) THEN
    ? loTokens.LastErrorText
    RELEASE loTokens
    RELEASE loOauth2
    CANCEL
ENDIF

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

RELEASE loTokens
RELEASE loOauth2