SQL Server Google Drive: Export File to Requested MIME Type (Export to PDF)

Back to Index

Exports a Google Doc to the requested MIME type and returns the exported content. Please note that the exported content is limited to 10MB.
(Export only supports Google Docs) See https://developers.google.com/drive/v3/web/manage-downloads for a list of MIME types.

Documentation: https://developers.google.com/drive/v3/reference/files/export


CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @rest int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Rest', @rest OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

    --   Provide a previously obtained OAuth2 access token.
    DECLARE @oauth2 int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.OAuth2', @oauth2 OUT

    EXEC sp_OASetProperty @oauth2, 'AccessToken', 'OAUTH2_ACCESS_TOKEN'
    EXEC sp_OAMethod @rest, 'SetAuthOAuth2', @success OUT, STR(@oauth2)

    EXEC sp_OAMethod @rest, 'Connect', @success OUT, 'www.googleapis.com', 443, 1, 1
    IF STR(@success) <> 1
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @oauth2
        RETURN
      END

    EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'mimeType', 'application/pdf'

    --  First send the HTTP request.
    EXEC sp_OAMethod @rest, 'SendReqNoBody', @success OUT, 'GET', '/drive/v3/files/1zvzpTHO1dM9vNKYxpmr4YrX7x_R6qiNGFb8WB8h9wuI/export'
    IF STR(@success) <> 1
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @oauth2
        RETURN
      END

    --  Read the response header.  If the response status code is success, stream to the file.
    --  Otherwise receive the error response text.
    DECLARE @statusCode int
    EXEC sp_OAMethod @rest, 'ReadResponseHeader', @statusCode OUT
    IF STR(@statusCode) < 0
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @oauth2
        RETURN
      END
    IF STR(@statusCode) <> 200
      BEGIN

        PRINT 'Received error response code: ' + STR(@statusCode)
        --  Read the error response body.
        DECLARE @sbErrResponse int
        EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbErrResponse OUT

        EXEC sp_OAMethod @rest, 'ReadRespSb', @success OUT, STR(@sbErrResponse)
        IF STR(@success) <> 1
          BEGIN
            EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
            PRINT @sTmp0
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @oauth2
            EXEC @hr = sp_OADestroy @sbErrResponse
            RETURN
          END

        EXEC sp_OAMethod @sbErrResponse, 'GetAsString', @sTmp0 OUT
        PRINT 'Error response:' + @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @oauth2
        EXEC @hr = sp_OADestroy @sbErrResponse
        RETURN
      END
    --  Stream the response body to the output file.
    DECLARE @respBodyStream int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Stream', @respBodyStream OUT

    EXEC sp_OASetProperty @respBodyStream, 'SinkFile', '/someDirectoryPath/test.pdf'
    EXEC sp_OAMethod @rest, 'ReadRespBodyStream', @success OUT, STR(@respBodyStream), 1
    IF STR(@success) <> 1
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @oauth2
        EXEC @hr = sp_OADestroy @sbErrResponse
        EXEC @hr = sp_OADestroy @respBodyStream
        RETURN
      END


    PRINT 'Example Completed.'

    EXEC @hr = sp_OADestroy @rest
    EXEC @hr = sp_OADestroy @oauth2
    EXEC @hr = sp_OADestroy @sbErrResponse
    EXEC @hr = sp_OADestroy @respBodyStream


END
GO