VBScript 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


Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set outFile = fso.CreateTextFile("output.txt", True)

set rest = CreateObject("Chilkat_9_5_0.Rest")

'   Provide a previously obtained OAuth2 access token.
set oauth2 = CreateObject("Chilkat_9_5_0.OAuth2")
oauth2.AccessToken = "OAUTH2_ACCESS_TOKEN"
success = rest.SetAuthOAuth2(oauth2)

success = rest.Connect("www.googleapis.com",443,1,1)
If (success <> 1) Then
    outFile.WriteLine(rest.LastErrorText)
    WScript.Quit
End If

success = rest.AddQueryParam("mimeType","application/pdf")

'  First send the HTTP request.
success = rest.SendReqNoBody("GET","/drive/v3/files/1zvzpTHO1dM9vNKYxpmr4YrX7x_R6qiNGFb8WB8h9wuI/export")
If (success <> 1) Then
    outFile.WriteLine(rest.LastErrorText)
    WScript.Quit
End If

'  Read the response header.  If the response status code is success, stream to the file.
'  Otherwise receive the error response text.
statusCode = rest.ReadResponseHeader()
If (statusCode < 0) Then
    outFile.WriteLine(rest.LastErrorText)
    WScript.Quit
End If

If (statusCode <> 200) Then
    outFile.WriteLine("Received error response code: " & statusCode)
    '  Read the error response body.
    set sbErrResponse = CreateObject("Chilkat_9_5_0.StringBuilder")
    success = rest.ReadRespSb(sbErrResponse)
    If (success <> 1) Then
        outFile.WriteLine(rest.LastErrorText)
        WScript.Quit
    End If

    outFile.WriteLine("Error response:" & sbErrResponse.GetAsString())
    WScript.Quit
End If

'  Stream the response body to the output file.
set respBodyStream = CreateObject("Chilkat_9_5_0.Stream")
respBodyStream.SinkFile = "/someDirectoryPath/test.pdf"
success = rest.ReadRespBodyStream(respBodyStream,1)
If (success <> 1) Then
    outFile.WriteLine(rest.LastErrorText)
    WScript.Quit
End If

outFile.WriteLine("Example Completed.")

outFile.Close