Classic ASP Requires Chilkat v11.0.0+
Classic ASP
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 Classic ASP Downloads
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0
' Assumes Chilkat is unlocked. See Global Unlock Sample.
set token = Server.CreateObject("Chilkat.JsonObject")
success = token.LoadFile("gmail-token.json")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not read gmail-token.json from the private working directory.") & "</pre>"
Response.End
End If
If ((token.JsonTypeOf("refresh_token") <> 1) Or (token.StringOfEquals("refresh_token","",1) = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Authorize the account for offline access first.") & "</pre>"
Response.End
End If
set oauth = Server.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
Response.Write "<pre>" & Server.HTMLEncode( "Refresh failed. Check the OAuth client and grant. A revoked or expired grant requires fresh consent.") & "</pre>"
Response.End
End If
set fresh = Server.CreateObject("Chilkat.JsonObject")
success = fresh.Load(oauth.AccessTokenResponse)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not parse the refresh response.") & "</pre>"
Response.End
End If
If ((fresh.JsonTypeOf("access_token") <> 1) Or (fresh.StringOfEquals("access_token","",1) = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "The refresh response did not contain an access token.") & "</pre>"
Response.End
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
Response.Write "<pre>" & Server.HTMLEncode( "Could not save the refreshed credentials.") & "</pre>"
Response.End
End If
Response.Write "<pre>" & Server.HTMLEncode( "Refreshed the access token.") & "</pre>"
%>
</body>
</html>