VBScript Requires Chilkat v11.0.0+
VBScript
Refresh a Gmail OAuth access token
See more GMail REST API Examples
Use the refresh token and the same Desktop app client that issued it. Run in the private directory used by the authorization example. Preserve the refresh token if Google omits it from the response. Refreshing continues the existing grant. Request additional scopes through interactive authorization.
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
' Assumes Chilkat is unlocked. See Global Unlock Sample.
set token = CreateObject("Chilkat.JsonObject")
success = token.LoadFile("gmail-token.json")
If (success <> 1) Then
outFile.WriteLine("Could not read gmail-token.json from the private working directory.")
WScript.Quit
End If
If ((token.JsonTypeOf("refresh_token") <> 1) Or (token.StringOfEquals("refresh_token","",1) = 1)) Then
outFile.WriteLine("Authorize the account for offline access first.")
WScript.Quit
End If
set oauth = CreateObject("Chilkat.OAuth2")
oauth.TokenEndpoint = "https://oauth2.googleapis.com/token"
oauth.ClientId = "YOUR_DESKTOP_CLIENT_ID"
oauth.ClientSecret = "YOUR_DESKTOP_CLIENT_SECRET"
oauth.RefreshToken = token.StringOf("refresh_token")
success = oauth.RefreshAccessToken()
If (success <> 1) Then
outFile.WriteLine("Refresh failed. Check the OAuth client and grant. A revoked or expired grant requires fresh consent.")
WScript.Quit
End If
set fresh = CreateObject("Chilkat.JsonObject")
success = fresh.Load(oauth.AccessTokenResponse)
If (success <> 1) Then
outFile.WriteLine("Could not parse the refresh response.")
WScript.Quit
End If
If ((fresh.JsonTypeOf("access_token") <> 1) Or (fresh.StringOfEquals("access_token","",1) = 1)) Then
outFile.WriteLine("The refresh response did not contain an access token.")
WScript.Quit
End If
If ((fresh.JsonTypeOf("refresh_token") <> 1) Or (fresh.StringOfEquals("refresh_token","",1) = 1)) Then
success = fresh.UpdateString("refresh_token",token.StringOf("refresh_token"))
End If
If ((fresh.HasMember("scope") <> 1) And (token.JsonTypeOf("scope") = 1)) Then
success = fresh.UpdateString("scope",token.StringOf("scope"))
End If
success = fresh.WriteFile("gmail-token.json")
If (success <> 1) Then
outFile.WriteLine("Could not save the refreshed credentials.")
WScript.Quit
End If
outFile.WriteLine("Refreshed the access token.")
outFile.Close