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 DECLARE @iTmp0 int DECLARE @sTmp0 nvarchar(4000) DECLARE @success int SELECT @success = 1 -- It requires the Chilkat API to have been previously unlocked. -- See Global Unlock Sample for sample code. -- 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_9_5_0.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_9_5_0.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 DECLARE @rest int EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Rest', @rest OUT -- Connect using TLS. DECLARE @bAutoReconnect int SELECT @bAutoReconnect = 1 EXEC sp_OAMethod @rest, 'Connect', @success OUT, 'www.googleapis.com', 443, 1, @bAutoReconnect -- Provide the authentication credentials (i.e. the access token) EXEC sp_OAMethod @rest, 'SetAuthOAuth2', @success OUT, @oauth2 -- We'll test with a simple request to empty trash. -- If our access token expired, we'll get a 401 response.. DECLARE @jsonResponse nvarchar(4000) EXEC sp_OAMethod @rest, 'FullRequestNoBody', @jsonResponse OUT, 'DELETE', '/drive/v3/files/trash' EXEC sp_OAGetProperty @rest, 'LastMethodSuccess', @iTmp0 OUT IF @iTmp0 <> 1 BEGIN EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @json EXEC @hr = sp_OADestroy @oauth2 EXEC @hr = sp_OADestroy @rest RETURN END -- If the access token expired, we'll get a 401 response status with this response body: -- { -- "error": { -- "errors": [ -- { -- "domain": "global", -- "reason": "authError", -- "message": "Invalid Credentials", -- "locationType": "header", -- "location": "Authorization" -- } -- ], -- "code": 401, -- "message": "Invalid Credentials" -- } -- } EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT IF @iTmp0 = 401 BEGIN PRINT 'Refreshing access token...' 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 EXEC @hr = sp_OADestroy @rest RETURN END -- Save the new access token to our XML 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_9_5_0.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!' -- Retry the request with the new access token.. EXEC sp_OAMethod @rest, 'FullRequestNoBody', @jsonResponse OUT, 'DELETE', '/drive/v3/files/trash' EXEC sp_OAGetProperty @rest, 'LastMethodSuccess', @iTmp0 OUT IF @iTmp0 <> 1 BEGIN EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @json EXEC @hr = sp_OADestroy @oauth2 EXEC @hr = sp_OADestroy @rest EXEC @hr = sp_OADestroy @fac RETURN END END -- A successful response will have a status code equal to 204 and the response body is empty. -- (If not successful, then there should be a JSON response body with information..) EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT IF @iTmp0 <> 204 BEGIN EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT PRINT 'response status code = ' + @iTmp0 EXEC sp_OAGetProperty @rest, 'ResponseStatusText', @sTmp0 OUT PRINT 'response status text = ' + @sTmp0 EXEC sp_OAGetProperty @rest, 'ResponseHeader', @sTmp0 OUT PRINT 'response header: ' + @sTmp0 PRINT 'response JSON: ' + @jsonResponse EXEC @hr = sp_OADestroy @json EXEC @hr = sp_OADestroy @oauth2 EXEC @hr = sp_OADestroy @rest EXEC @hr = sp_OADestroy @fac RETURN END PRINT 'Trash Emptied!' EXEC @hr = sp_OADestroy @json EXEC @hr = sp_OADestroy @oauth2 EXEC @hr = sp_OADestroy @rest EXEC @hr = sp_OADestroy @fac END GO |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.