PowerBuilder 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


integer li_rc
oleobject loo_Rest
integer li_Success
oleobject loo_Oauth2
integer li_StatusCode
oleobject loo_SbErrResponse
oleobject loo_RespBodyStream

loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat_9_5_0.Rest")
if li_rc < 0 then
    destroy loo_Rest
    MessageBox("Error","Connecting to COM object failed")
    return
end if

//   Provide a previously obtained OAuth2 access token.
loo_Oauth2 = create oleobject
li_rc = loo_Oauth2.ConnectToNewObject("Chilkat_9_5_0.OAuth2")

loo_Oauth2.AccessToken = "OAUTH2_ACCESS_TOKEN"
loo_Rest.SetAuthOAuth2(loo_Oauth2)

li_Success = loo_Rest.Connect("www.googleapis.com",443,1,1)
if li_Success <> 1 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    destroy loo_Oauth2
    return
end if

loo_Rest.AddQueryParam("mimeType","application/pdf")

//  First send the HTTP request.
li_Success = loo_Rest.SendReqNoBody("GET","/drive/v3/files/1zvzpTHO1dM9vNKYxpmr4YrX7x_R6qiNGFb8WB8h9wuI/export")
if li_Success <> 1 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    destroy loo_Oauth2
    return
end if

//  Read the response header.  If the response status code is success, stream to the file.
//  Otherwise receive the error response text.
li_StatusCode = loo_Rest.ReadResponseHeader()
if li_StatusCode < 0 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    destroy loo_Oauth2
    return
end if

if li_StatusCode <> 200 then
    Write-Debug "Received error response code: " + string(li_StatusCode)
    //  Read the error response body.
    loo_SbErrResponse = create oleobject
    li_rc = loo_SbErrResponse.ConnectToNewObject("Chilkat_9_5_0.StringBuilder")

    li_Success = loo_Rest.ReadRespSb(loo_SbErrResponse)
    if li_Success <> 1 then
        Write-Debug loo_Rest.LastErrorText
        destroy loo_Rest
        destroy loo_Oauth2
        destroy loo_SbErrResponse
        return
    end if

    Write-Debug "Error response:" + loo_SbErrResponse.GetAsString()
    destroy loo_Rest
    destroy loo_Oauth2
    destroy loo_SbErrResponse
    return
end if

//  Stream the response body to the output file.
loo_RespBodyStream = create oleobject
li_rc = loo_RespBodyStream.ConnectToNewObject("Chilkat_9_5_0.Stream")

loo_RespBodyStream.SinkFile = "/someDirectoryPath/test.pdf"
li_Success = loo_Rest.ReadRespBodyStream(loo_RespBodyStream,1)
if li_Success <> 1 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    destroy loo_Oauth2
    destroy loo_SbErrResponse
    destroy loo_RespBodyStream
    return
end if

Write-Debug "Example Completed."


destroy loo_Rest
destroy loo_Oauth2
destroy loo_SbErrResponse
destroy loo_RespBodyStream