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 Tcl Downloads
load ./chilkat.dll
set success 0
# Assumes Chilkat is unlocked. See Global Unlock Sample.
set token [new_CkJsonObject]
set success [CkJsonObject_LoadFile $token "gmail-mail-token.json"]
if {$success != 1} then {
puts "Could not read gmail-mail-token.json from the private working directory."
delete_CkJsonObject $token
exit
}
if {expr [[CkJsonObject_JsonTypeOf $token "refresh_token"] != 1] || [[CkJsonObject_StringOfEquals $token "refresh_token" "" 1] == 1]} then {
puts "Authorize the account for offline access first."
delete_CkJsonObject $token
exit
}
set oauth [new_CkOAuth2]
CkOAuth2_put_TokenEndpoint $oauth "https://oauth2.googleapis.com/token"
CkOAuth2_put_ClientId $oauth "YOUR_DESKTOP_CLIENT_ID"
CkOAuth2_put_ClientSecret $oauth "YOUR_DESKTOP_CLIENT_SECRET"
CkOAuth2_put_RefreshToken $oauth [CkJsonObject_stringOf $token "refresh_token"]
set success [CkOAuth2_RefreshAccessToken $oauth]
if {$success != 1} then {
puts "Refresh failed. Check the OAuth client and grant. A revoked or expired grant requires fresh consent."
delete_CkJsonObject $token
delete_CkOAuth2 $oauth
exit
}
set fresh [new_CkJsonObject]
set success [CkJsonObject_Load $fresh [CkOAuth2_accessTokenResponse $oauth]]
if {$success != 1} then {
puts "Could not parse the refresh response."
delete_CkJsonObject $token
delete_CkOAuth2 $oauth
delete_CkJsonObject $fresh
exit
}
if {expr [[CkJsonObject_JsonTypeOf $fresh "access_token"] != 1] || [[CkJsonObject_StringOfEquals $fresh "access_token" "" 1] == 1]} then {
puts "The refresh response did not contain an access token."
delete_CkJsonObject $token
delete_CkOAuth2 $oauth
delete_CkJsonObject $fresh
exit
}
if {expr [[CkJsonObject_JsonTypeOf $fresh "refresh_token"] != 1] || [[CkJsonObject_StringOfEquals $fresh "refresh_token" "" 1] == 1]} then {
CkJsonObject_UpdateString $fresh "refresh_token" [CkJsonObject_stringOf $token "refresh_token"]
}
if {expr [[CkJsonObject_HasMember $fresh "scope"] != 1] && [[CkJsonObject_JsonTypeOf $token "scope"] == 1]} then {
CkJsonObject_UpdateString $fresh "scope" [CkJsonObject_stringOf $token "scope"]
}
# 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.
set obtained [new_CkDateTime]
CkDateTime_SetFromCurrentSystemTime $obtained
CkJsonObject_UpdateString $fresh "obtained_at" [CkDateTime_getAsTimestamp $obtained 0]
set success [CkJsonObject_WriteFile $fresh "gmail-mail-token.json"]
if {$success != 1} then {
puts "Could not save the refreshed credentials."
delete_CkJsonObject $token
delete_CkOAuth2 $oauth
delete_CkJsonObject $fresh
delete_CkDateTime $obtained
exit
}
puts "Refreshed the access token."
delete_CkJsonObject $token
delete_CkOAuth2 $oauth
delete_CkJsonObject $fresh
delete_CkDateTime $obtained