VB.NET Requires Chilkat v11.0.0+
VB.NET
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 VB.NET Downloads
Dim success As Boolean = False
' Assumes Chilkat is unlocked. See Global Unlock Sample.
Dim token As New Chilkat.JsonObject
success = token.LoadFile("gmail-token.json")
If (success <> True) Then
Debug.WriteLine("Could not read gmail-token.json from the private working directory.")
Exit Sub
End If
If ((token.JsonTypeOf("refresh_token") <> 1) Or (token.StringOfEquals("refresh_token","",True) = True)) Then
Debug.WriteLine("Authorize the account for offline access first.")
Exit Sub
End If
Dim oauth As New 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 <> True) Then
Debug.WriteLine("Refresh failed. Check the OAuth client and grant. A revoked or expired grant requires fresh consent.")
Exit Sub
End If
Dim fresh As New Chilkat.JsonObject
success = fresh.Load(oauth.AccessTokenResponse)
If (success <> True) Then
Debug.WriteLine("Could not parse the refresh response.")
Exit Sub
End If
If ((fresh.JsonTypeOf("access_token") <> 1) Or (fresh.StringOfEquals("access_token","",True) = True)) Then
Debug.WriteLine("The refresh response did not contain an access token.")
Exit Sub
End If
If ((fresh.JsonTypeOf("refresh_token") <> 1) Or (fresh.StringOfEquals("refresh_token","",True) = True)) Then
fresh.UpdateString("refresh_token",token.StringOf("refresh_token"))
End If
If ((fresh.HasMember("scope") <> True) And (token.JsonTypeOf("scope") = 1)) Then
fresh.UpdateString("scope",token.StringOf("scope"))
End If
success = fresh.WriteFile("gmail-token.json")
If (success <> True) Then
Debug.WriteLine("Could not save the refreshed credentials.")
Exit Sub
End If
Debug.WriteLine("Refreshed the access token.")