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 SQL Server Downloads
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
DECLARE @iTmp1 int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
DECLARE @token int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @token OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OAMethod @token, 'LoadFile', @success OUT, 'gmail-mail-token.json'
IF @success <> 1
BEGIN
PRINT 'Could not read gmail-mail-token.json from the private working directory.'
EXEC @hr = sp_OADestroy @token
RETURN
END
EXEC sp_OAMethod @token, 'JsonTypeOf', @iTmp0 OUT, 'refresh_token'
EXEC sp_OAMethod @token, 'StringOfEquals', @iTmp1 OUT, 'refresh_token', '', 1
IF (@iTmp0 <> 1) or (@iTmp1 = 1)
BEGIN
PRINT 'Authorize the account for offline access first.'
EXEC @hr = sp_OADestroy @token
RETURN
END
DECLARE @oauth int
EXEC @hr = sp_OACreate 'Chilkat.OAuth2', @oauth OUT
EXEC sp_OASetProperty @oauth, 'TokenEndpoint', 'https://oauth2.googleapis.com/token'
EXEC sp_OASetProperty @oauth, 'ClientId', 'YOUR_DESKTOP_CLIENT_ID'
EXEC sp_OASetProperty @oauth, 'ClientSecret', 'YOUR_DESKTOP_CLIENT_SECRET'
EXEC sp_OAMethod @token, 'StringOf', @sTmp0 OUT, 'refresh_token'
EXEC sp_OASetProperty @oauth, 'RefreshToken', @sTmp0
EXEC sp_OAMethod @oauth, 'RefreshAccessToken', @success OUT
IF @success <> 1
BEGIN
PRINT 'Refresh failed. Check the OAuth client and grant. A revoked or expired grant requires fresh consent.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @oauth
RETURN
END
DECLARE @fresh int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @fresh OUT
EXEC sp_OAGetProperty @oauth, 'AccessTokenResponse', @sTmp0 OUT
EXEC sp_OAMethod @fresh, 'Load', @success OUT, @sTmp0
IF @success <> 1
BEGIN
PRINT 'Could not parse the refresh response.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @oauth
EXEC @hr = sp_OADestroy @fresh
RETURN
END
EXEC sp_OAMethod @fresh, 'JsonTypeOf', @iTmp0 OUT, 'access_token'
EXEC sp_OAMethod @fresh, 'StringOfEquals', @iTmp1 OUT, 'access_token', '', 1
IF (@iTmp0 <> 1) or (@iTmp1 = 1)
BEGIN
PRINT 'The refresh response did not contain an access token.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @oauth
EXEC @hr = sp_OADestroy @fresh
RETURN
END
EXEC sp_OAMethod @fresh, 'JsonTypeOf', @iTmp0 OUT, 'refresh_token'
EXEC sp_OAMethod @fresh, 'StringOfEquals', @iTmp1 OUT, 'refresh_token', '', 1
IF (@iTmp0 <> 1) or (@iTmp1 = 1)
BEGIN
EXEC sp_OAMethod @token, 'StringOf', @sTmp0 OUT, 'refresh_token'
EXEC sp_OAMethod @fresh, 'UpdateString', @success OUT, 'refresh_token', @sTmp0
END
EXEC sp_OAMethod @fresh, 'HasMember', @iTmp0 OUT, 'scope'
EXEC sp_OAMethod @token, 'JsonTypeOf', @iTmp1 OUT, 'scope'
IF (@iTmp0 <> 1) and (@iTmp1 = 1)
BEGIN
EXEC sp_OAMethod @token, 'StringOf', @sTmp0 OUT, 'scope'
EXEC sp_OAMethod @fresh, 'UpdateString', @success OUT, 'scope', @sTmp0
END
-- 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.
DECLARE @obtained int
EXEC @hr = sp_OACreate 'Chilkat.CkDateTime', @obtained OUT
EXEC sp_OAMethod @obtained, 'SetFromCurrentSystemTime', @success OUT
EXEC sp_OAMethod @obtained, 'GetAsTimestamp', @sTmp0 OUT, 0
EXEC sp_OAMethod @fresh, 'UpdateString', @success OUT, 'obtained_at', @sTmp0
EXEC sp_OAMethod @fresh, 'WriteFile', @success OUT, 'gmail-mail-token.json'
IF @success <> 1
BEGIN
PRINT 'Could not save the refreshed credentials.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @oauth
EXEC @hr = sp_OADestroy @fresh
EXEC @hr = sp_OADestroy @obtained
RETURN
END
PRINT 'Refreshed the access token.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @oauth
EXEC @hr = sp_OADestroy @fresh
EXEC @hr = sp_OADestroy @obtained
END
GO