DataFlex 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


Use ChilkatAx-9.5.0-win32.pkg

Procedure Test
    Handle hoRest
    Boolean iSuccess
    Variant vOauth2
    Handle hoOauth2
    Integer iStatusCode
    Variant vSbErrResponse
    Handle hoSbErrResponse
    Variant vRespBodyStream
    Handle hoRespBodyStream
    String sTemp1

    Get Create (RefClass(cComChilkatRest)) To hoRest
    If (Not(IsComObjectCreated(hoRest))) Begin
        Send CreateComObject of hoRest
    End

    //   Provide a previously obtained OAuth2 access token.
    Get Create (RefClass(cComChilkatOAuth2)) To hoOauth2
    If (Not(IsComObjectCreated(hoOauth2))) Begin
        Send CreateComObject of hoOauth2
    End
    Set ComAccessToken Of hoOauth2 To "OAUTH2_ACCESS_TOKEN"
    Get pvComObject of hoOauth2 to vOauth2
    Get ComSetAuthOAuth2 Of hoRest vOauth2 To iSuccess

    Get ComConnect Of hoRest "www.googleapis.com" 443 True True To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComAddQueryParam Of hoRest "mimeType" "application/pdf" To iSuccess

    //  First send the HTTP request.
    Get ComSendReqNoBody Of hoRest "GET" "/drive/v3/files/1zvzpTHO1dM9vNKYxpmr4YrX7x_R6qiNGFb8WB8h9wuI/export" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Read the response header.  If the response status code is success, stream to the file.
    //  Otherwise receive the error response text.
    Get ComReadResponseHeader Of hoRest To iStatusCode
    If (iStatusCode < 0) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    If (iStatusCode <> 200) Begin
        Showln "Received error response code: " iStatusCode
        //  Read the error response body.
        Get Create (RefClass(cComChilkatStringBuilder)) To hoSbErrResponse
        If (Not(IsComObjectCreated(hoSbErrResponse))) Begin
            Send CreateComObject of hoSbErrResponse
        End
        Get pvComObject of hoSbErrResponse to vSbErrResponse
        Get ComReadRespSb Of hoRest vSbErrResponse To iSuccess
        If (iSuccess <> True) Begin
            Get ComLastErrorText Of hoRest To sTemp1
            Showln sTemp1
            Procedure_Return
        End

        Get ComGetAsString Of hoSbErrResponse To sTemp1
        Showln "Error response:" sTemp1
        Procedure_Return
    End

    //  Stream the response body to the output file.
    Get Create (RefClass(cComChilkatStream)) To hoRespBodyStream
    If (Not(IsComObjectCreated(hoRespBodyStream))) Begin
        Send CreateComObject of hoRespBodyStream
    End
    Set ComSinkFile Of hoRespBodyStream To "/someDirectoryPath/test.pdf"
    Get pvComObject of hoRespBodyStream to vRespBodyStream
    Get ComReadRespBodyStream Of hoRest vRespBodyStream True To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Example Completed."


End_Procedure