Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(SQL Server) Google Drive Refresh Access TokenDemonstrates how to automatically refresh the access token when it expires.
-- 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) -- This example requires the Chilkat API to have been previously unlocked. -- See Global Unlock Sample for sample code. DECLARE @success int 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 -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 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 -- Use "Chilkat_9_5_0.OAuth2" for versions of Chilkat < 10.0.0 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 -- Use "Chilkat_9_5_0.FileAccess" for versions of Chilkat < 10.0.0 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 |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.