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 Multipart Upload StringDemonstrates a file upload to Google Drive where the contents of the file are contained in a string variable.
-- 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 -- Important: Do not use nvarchar(max). See the warning about using nvarchar(max). DECLARE @sTmp0 nvarchar(4000) -- This example will upload a file to Google Drive. DECLARE @success int SELECT @success = 1 -- It requires the Chilkat API to have been previously unlocked. -- See Global Unlock Sample for sample code. DECLARE @gAuth int -- Use "Chilkat_9_5_0.AuthGoogle" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.AuthGoogle', @gAuth OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END EXEC sp_OASetProperty @gAuth, 'Scope', 'https://www.googleapis.com/auth/drive' EXEC sp_OASetProperty @gAuth, 'SubEmailAddress', 'some.user@example.com' EXEC sp_OASetProperty @gAuth, 'ExpireNumSeconds', 3600 -- Obtain an access token as shown in one of the following examples: -- See Get Access Token using a Service Account JSON Key -- See Get Access Token using a P12 File DECLARE @rest int -- Use "Chilkat_9_5_0.Rest" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.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 key) EXEC sp_OAMethod @rest, 'SetAuthGoogle', @success OUT, @gAuth -- A multipart upload to Google Drive needs a multipart/related Content-Type EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Content-Type', 'multipart/related' -- Specify each part of the request. -- The 1st part is JSON with information about the file. EXEC sp_OASetProperty @rest, 'PartSelector', '1' EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Content-Type', 'application/json; charset=UTF-8' DECLARE @json int -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT EXEC sp_OAMethod @json, 'AddStringAt', @success OUT, -1, 'title', 'helloWorld.txt' EXEC sp_OAMethod @json, 'AddStringAt', @success OUT, -1, 'description', 'A simple text file that says Hello World.' EXEC sp_OAMethod @json, 'AddStringAt', @success OUT, -1, 'mimeType', 'text/plain' EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT EXEC sp_OAMethod @rest, 'SetMultipartBodyString', @success OUT, @sTmp0 -- The 2nd part is the file content. -- In this case, we'll upload a simple text file containing "Hello World!" EXEC sp_OASetProperty @rest, 'PartSelector', '2' EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Content-Type', 'text/plain' EXEC sp_OAMethod @rest, 'SetMultipartBodyString', @success OUT, 'Hello World!' -- POST https://www.googleapis.com/upload/drive/v2/files DECLARE @jsonResponse nvarchar(4000) EXEC sp_OAMethod @rest, 'FullRequestMultipart', @jsonResponse OUT, 'POST', '/upload/drive/v2/files?uploadType=multipart' EXEC sp_OAGetProperty @rest, 'LastMethodSuccess', @iTmp0 OUT IF @iTmp0 <> 1 BEGIN EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @gAuth EXEC @hr = sp_OADestroy @rest EXEC @hr = sp_OADestroy @json RETURN END -- Show the JSON response. EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT PRINT 'Response Status Code: ' + @iTmp0 PRINT 'Json Response: ' + @jsonResponse EXEC @hr = sp_OADestroy @gAuth EXEC @hr = sp_OADestroy @rest EXEC @hr = sp_OADestroy @json END GO |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.