Authorize a desktop application to use Gmail
See more GMail REST API Examples
Enable the Gmail API, configure the OAuth consent screen, and create a Desktop app OAuth client. Launch the system browser and receive the authorization code on a loopback listener. The sample requests read-only access with PKCE and offline access. For another operation, select the scope in that example before authorizing. Gmail scopes.
Run in a private, user-only directory. The file contains bearer credentials. In a deployed application use protected credential storage. To reuse an existing grant, copy the older examples’ qa_data/tokens/gmail.json to gmail-token.json in this private directory. The token retains its original scopes. Gmail clients using IMAP, POP, or SMTP with OAuth need the https://mail.google.com/ scope. Use a separate cache for each user and client.
Chilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
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 @oauth int
EXEC @hr = sp_OACreate 'Chilkat.OAuth2', @oauth OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
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_OASetProperty @oauth, 'AuthorizationEndpoint', 'https://accounts.google.com/o/oauth2/v2/auth'
EXEC sp_OASetProperty @oauth, 'LocalHost', '127.0.0.1'
EXEC sp_OASetProperty @oauth, 'ListenPort', 55568
EXEC sp_OASetProperty @oauth, 'ListenPortRangeEnd', 55578
EXEC sp_OASetProperty @oauth, 'CodeChallenge', 1
EXEC sp_OASetProperty @oauth, 'CodeChallengeMethod', 'S256'
EXEC sp_OASetProperty @oauth, 'Scope', 'https://www.googleapis.com/auth/gmail.readonly'
EXEC sp_OAMethod @oauth, 'AddAuthQueryParam', @success OUT, 'access_type', 'offline'
IF @success <> 1
BEGIN
PRINT 'Could not configure offline access.'
EXEC @hr = sp_OADestroy @oauth
RETURN
END
-- This is the interactive setup step. Subsequent refreshes use the saved refresh token.
EXEC sp_OAMethod @oauth, 'AddAuthQueryParam', @success OUT, 'prompt', 'consent'
IF @success <> 1
BEGIN
PRINT 'Could not configure consent.'
EXEC @hr = sp_OADestroy @oauth
RETURN
END
DECLARE @authUrl nvarchar(4000)
EXEC sp_OAMethod @oauth, 'StartAuth', @authUrl OUT
EXEC sp_OAGetProperty @oauth, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 <> 1
BEGIN
PRINT 'Could not start the loopback listener.'
EXEC @hr = sp_OADestroy @oauth
RETURN
END
EXEC sp_OAMethod @oauth, 'LaunchBrowser', @success OUT, @authUrl
IF @success <> 1
BEGIN
EXEC sp_OAMethod @oauth, 'Cancel', @success OUT
PRINT 'Could not open the system browser.'
EXEC @hr = sp_OADestroy @oauth
RETURN
END
DECLARE @waited int
SELECT @waited = 0
EXEC sp_OAGetProperty @oauth, 'AuthFlowState', @iTmp0 OUT
WHILE ((@iTmp0 < 3) and (@waited < 180000))
BEGIN
EXEC sp_OAMethod @oauth, 'SleepMs', NULL, 200
SELECT @waited = @waited + 200
END
EXEC sp_OAGetProperty @oauth, 'AuthFlowState', @iTmp0 OUT
IF @iTmp0 < 3
BEGIN
EXEC sp_OAMethod @oauth, 'Cancel', @success OUT
PRINT 'Authorization timed out.'
EXEC @hr = sp_OADestroy @oauth
RETURN
END
EXEC sp_OAGetProperty @oauth, 'AuthFlowState', @iTmp0 OUT
IF @iTmp0 <> 3
BEGIN
EXEC sp_OAGetProperty @oauth, 'AuthFlowState', @iTmp0 OUT
PRINT 'Authorization did not complete. Flow state: ' + @iTmp0
EXEC @hr = sp_OADestroy @oauth
RETURN
END
DECLARE @token int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @token OUT
EXEC sp_OAGetProperty @oauth, 'AccessTokenResponse', @sTmp0 OUT
EXEC sp_OAMethod @token, 'Load', @success OUT, @sTmp0
IF @success <> 1
BEGIN
PRINT 'Could not parse the token response.'
EXEC @hr = sp_OADestroy @oauth
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 'No offline refresh token was issued. The existing cache was preserved.'
EXEC @hr = sp_OADestroy @oauth
EXEC @hr = sp_OADestroy @token
RETURN
END
EXEC sp_OAMethod @token, 'WriteFile', @success OUT, 'gmail-token.json'
IF @success <> 1
BEGIN
PRINT 'Could not save the token file.'
EXEC @hr = sp_OADestroy @oauth
EXEC @hr = sp_OADestroy @token
RETURN
END
PRINT 'Credentials saved in the private working directory.'
EXEC @hr = sp_OADestroy @oauth
EXEC @hr = sp_OADestroy @token
END
GO