SQL Server
SQL Server
Google Drive Refresh Access Token
See more Google Drive Examples
Demonstrates how to automatically refresh the access token when it expires.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
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
SELECT @success = 1
-- This example uses a previously obtained access token having permission for the
-- Google Drive scope.
-- The access token (and refresh token) was previously saved to a JSON file with this format:
-- See Get Google Drive OAuth2 Access Token
-- {
-- "access_token": "ya29.Gls-BsdxTWuenChv ... yzVIrXikkLxu5T6dy4I6GjADFardoz4Lruw",
-- "expires_in": 3600,
-- "refresh_token": "1/tMBJ ... 27D-Hk6rpQYBA",
-- "scope": "https://www.googleapis.com/auth/drive",
-- "token_type": "Bearer"
-- }
DECLARE @json int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
DECLARE @tokenFilePath nvarchar(4000)
SELECT @tokenFilePath = 'qa_data/tokens/googleDrive.json'
EXEC sp_OAMethod @json, 'LoadFile', @success OUT, @tokenFilePath
DECLARE @oauth2 int
EXEC @hr = sp_OACreate 'Chilkat.OAuth2', @oauth2 OUT
EXEC sp_OAMethod @json, 'StringOf', @sTmp0 OUT, 'access_token'
EXEC sp_OASetProperty @oauth2, 'AccessToken', @sTmp0
EXEC sp_OAMethod @json, 'StringOf', @sTmp0 OUT, 'refresh_token'
EXEC sp_OASetProperty @oauth2, 'RefreshToken', @sTmp0
EXEC sp_OASetProperty @oauth2, 'AuthorizationEndpoint', 'https://accounts.google.com/o/oauth2/v2/auth'
EXEC sp_OASetProperty @oauth2, 'TokenEndpoint', 'https://www.googleapis.com/oauth2/v4/token'
-- Replace these with actual values.
EXEC sp_OASetProperty @oauth2, 'ClientId', 'GOOGLE-CLIENT-ID'
EXEC sp_OASetProperty @oauth2, 'ClientSecret', 'GOOGLE-CLIENT-SECRET'
EXEC sp_OASetProperty @oauth2, 'Scope', 'https://www.googleapis.com/auth/drive'
-- Use OAuth2 to refresh the access token.
EXEC sp_OAMethod @oauth2, 'RefreshAccessToken', @success OUT
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @oauth2, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @oauth2
RETURN
END
EXEC sp_OAGetProperty @oauth2, 'AccessTokenResponse', @sTmp0 OUT
PRINT @sTmp0
-- Save the new access token to our JSON file (so we can refresh it again when needed).
EXEC sp_OAGetProperty @oauth2, 'AccessToken', @sTmp0 OUT
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'access_token', @sTmp0
DECLARE @fac int
EXEC @hr = sp_OACreate 'Chilkat.FileAccess', @fac OUT
EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
EXEC sp_OAMethod @fac, 'WriteEntireTextFile', @success OUT, @tokenFilePath, @sTmp0, 'utf-8', 0
PRINT 'Access Token Refreshed!'
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @oauth2
EXEC @hr = sp_OADestroy @fac
END
GO