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

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Tokens
oleobject loo_Oauth2

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

loo_Oauth2 = create oleobject
li_rc = loo_Oauth2.ConnectToNewObject("Chilkat.OAuth2")

loo_Oauth2.TokenEndpoint = "https://api.box.com/oauth2/token"
loo_Oauth2.ClientId = "BOX_CLIENT_ID"
loo_Oauth2.ClientSecret = "BOX_CLIENT_SECRET"
loo_Oauth2.RefreshToken = loo_Tokens.StringOf("refresh_token")
li_Success = loo_Oauth2.RefreshAccessToken()
if li_Success <> 1 then
    Write-Debug loo_Oauth2.LastErrorText
    destroy loo_Tokens
    destroy loo_Oauth2
    return
end if

li_Success = loo_Tokens.Load(loo_Oauth2.AccessTokenResponse)
if li_Success <> 1 then
    Write-Debug loo_Tokens.LastErrorText
    destroy loo_Tokens
    destroy loo_Oauth2
    return
end if

//  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.
li_Success = loo_Tokens.WriteFile("qa_data/tokens/box_oauth.json")
if li_Success <> 1 then
    Write-Debug loo_Tokens.LastErrorText
    destroy loo_Tokens
    destroy loo_Oauth2
    return
end if

Write-Debug "New access and refresh tokens saved. Do not log their values."


destroy loo_Tokens
destroy loo_Oauth2