Sample code for 30+ languages & platforms
SQL Server

Google Drive Multipart Upload String

See more REST Examples

Demonstrates a file upload to Google Drive where the contents of the file are contained in a string variable.

Chilkat SQL Server Downloads

SQL Server
-- 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)
    DECLARE @success int
    SELECT @success = 0

    -- This example will upload a file to Google Drive.
    SELECT @success = 1

    -- It requires the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @gAuth int
    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
    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
    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