Gmail SMTP, IMAP, and POP3: Refresh an OAuth2 Token
See more OAuth2 Examples
Refresh a Gmail access token without opening a browser. Use the same OAuth client and the offline refresh token saved during initial authorization. A refreshed token can authenticate SMTP, IMAP, or POP3 for the authorized mailbox.
Google normally does not return another refresh token. The example retains the saved refresh token when omitted, accepts a replacement when provided, preserves the granted scope when omitted, and records the new access token's issue time and lifetime. A failed exchange leaves the old cache untouched. File writes are checked; production applications should use protected storage and serialize/atomically commit updates.
Access tokens normally last about an hour. Refresh ahead of expiry and reconnect long-lived protocol sessions when necessary. Refresh does not add scopes or restore a revoked grant. An invalid_grant response can require fresh user authorization; do not retry it indefinitely. External apps in Testing normally receive seven-day refresh tokens for this scope.
Gmail SMTP, IMAP, and POP3 use the https://mail.google.com/ OAuth scope. Narrower Gmail REST scopes such as gmail.send or gmail.readonly do not authorize these protocols. Public applications using this restricted scope must meet Google's verification requirements. See Gmail OAuth scopes and XOAUTH2.
Chilkat PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Token
oleobject loo_Oauth
oleobject loo_Fresh
oleobject loo_Obtained
li_Success = 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
loo_Token = create oleobject
li_rc = loo_Token.ConnectToNewObject("Chilkat.JsonObject")
if li_rc < 0 then
destroy loo_Token
MessageBox("Error","Connecting to COM object failed")
return
end if
li_Success = loo_Token.LoadFile("gmail-mail-token.json")
if li_Success <> 1 then
Write-Debug "Could not read gmail-mail-token.json from the private working directory."
destroy loo_Token
return
end if
if (loo_Token.JsonTypeOf("refresh_token") <> 1) OR (loo_Token.StringOfEquals("refresh_token","",1) = 1) then
Write-Debug "Authorize the account for offline access first."
destroy loo_Token
return
end if
loo_Oauth = create oleobject
li_rc = loo_Oauth.ConnectToNewObject("Chilkat.OAuth2")
loo_Oauth.TokenEndpoint = "https://oauth2.googleapis.com/token"
loo_Oauth.ClientId = "YOUR_DESKTOP_CLIENT_ID"
loo_Oauth.ClientSecret = "YOUR_DESKTOP_CLIENT_SECRET"
loo_Oauth.RefreshToken = loo_Token.StringOf("refresh_token")
li_Success = loo_Oauth.RefreshAccessToken()
if li_Success <> 1 then
Write-Debug "Refresh failed. Check the OAuth client and grant. A revoked or expired grant requires fresh consent."
destroy loo_Token
destroy loo_Oauth
return
end if
loo_Fresh = create oleobject
li_rc = loo_Fresh.ConnectToNewObject("Chilkat.JsonObject")
li_Success = loo_Fresh.Load(loo_Oauth.AccessTokenResponse)
if li_Success <> 1 then
Write-Debug "Could not parse the refresh response."
destroy loo_Token
destroy loo_Oauth
destroy loo_Fresh
return
end if
if (loo_Fresh.JsonTypeOf("access_token") <> 1) OR (loo_Fresh.StringOfEquals("access_token","",1) = 1) then
Write-Debug "The refresh response did not contain an access token."
destroy loo_Token
destroy loo_Oauth
destroy loo_Fresh
return
end if
if (loo_Fresh.JsonTypeOf("refresh_token") <> 1) OR (loo_Fresh.StringOfEquals("refresh_token","",1) = 1) then
loo_Fresh.UpdateString("refresh_token",loo_Token.StringOf("refresh_token"))
end if
if (loo_Fresh.HasMember("scope") <> 1) AND (loo_Token.JsonTypeOf("scope") = 1) then
loo_Fresh.UpdateString("scope",loo_Token.StringOf("scope"))
end if
// Google normally omits refresh_token on refresh. Keep the previous one in that case.
// Use the new expires_in, not the old response's expiration information.
loo_Obtained = create oleobject
li_rc = loo_Obtained.ConnectToNewObject("Chilkat.CkDateTime")
loo_Obtained.SetFromCurrentSystemTime()
loo_Fresh.UpdateString("obtained_at",loo_Obtained.GetAsTimestamp(0))
li_Success = loo_Fresh.WriteFile("gmail-mail-token.json")
if li_Success <> 1 then
Write-Debug "Could not save the refreshed credentials."
destroy loo_Token
destroy loo_Oauth
destroy loo_Fresh
destroy loo_Obtained
return
end if
Write-Debug "Refreshed the access token."
destroy loo_Token
destroy loo_Oauth
destroy loo_Fresh
destroy loo_Obtained