VBScript Requires Chilkat v11.5.0+
VBScript
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 VBScript Downloads
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)
success = 0
' Requires Chilkat 11.5.0 or later and a previously unlocked Chilkat bundle.
' See Global Unlock Sample.
' Guide: Box API Guide.
success = 0
set tokens = CreateObject("Chilkat.JsonObject")
success = tokens.LoadFile("qa_data/tokens/box_oauth.json")
If (success <> 1) Then
outFile.WriteLine(tokens.LastErrorText)
WScript.Quit
End If
set oauth2 = CreateObject("Chilkat.OAuth2")
oauth2.TokenEndpoint = "https://api.box.com/oauth2/token"
oauth2.ClientId = "BOX_CLIENT_ID"
oauth2.ClientSecret = "BOX_CLIENT_SECRET"
oauth2.RefreshToken = tokens.StringOf("refresh_token")
success = oauth2.RefreshAccessToken()
If (success <> 1) Then
outFile.WriteLine(oauth2.LastErrorText)
WScript.Quit
End If
success = tokens.Load(oauth2.AccessTokenResponse)
If (success <> 1) Then
outFile.WriteLine(tokens.LastErrorText)
WScript.Quit
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.
success = tokens.WriteFile("qa_data/tokens/box_oauth.json")
If (success <> 1) Then
outFile.WriteLine(tokens.LastErrorText)
WScript.Quit
End If
outFile.WriteLine("New access and refresh tokens saved. Do not log their values.")
outFile.Close