PureBasic 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


IncludeFile "CkStream.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkRest.pb"
IncludeFile "CkOAuth2.pb"

Procedure ChilkatExample()

    rest.i = CkRest::ckCreate()
    If rest.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success.i

    ;   Provide a previously obtained OAuth2 access token.
    oauth2.i = CkOAuth2::ckCreate()
    If oauth2.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkOAuth2::setCkAccessToken(oauth2, "OAUTH2_ACCESS_TOKEN")
    CkRest::ckSetAuthOAuth2(rest,oauth2)

    success = CkRest::ckConnect(rest,"www.googleapis.com",443,1,1)
    If success <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkOAuth2::ckDispose(oauth2)
        ProcedureReturn
    EndIf

    CkRest::ckAddQueryParam(rest,"mimeType","application/pdf")

    ;  First send the HTTP request.
    success = CkRest::ckSendReqNoBody(rest,"GET","/drive/v3/files/1zvzpTHO1dM9vNKYxpmr4YrX7x_R6qiNGFb8WB8h9wuI/export")
    If success <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkOAuth2::ckDispose(oauth2)
        ProcedureReturn
    EndIf

    ;  Read the response header.  If the response status code is success, stream to the file.
    ;  Otherwise receive the error response text.
    statusCode.i = CkRest::ckReadResponseHeader(rest)
    If statusCode < 0
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkOAuth2::ckDispose(oauth2)
        ProcedureReturn
    EndIf

    If statusCode <> 200
        Debug "Received error response code: " + Str(statusCode)
        ;  Read the error response body.
        sbErrResponse.i = CkStringBuilder::ckCreate()
        If sbErrResponse.i = 0
            Debug "Failed to create object."
            ProcedureReturn
        EndIf

        success = CkRest::ckReadRespSb(rest,sbErrResponse)
        If success <> 1
            Debug CkRest::ckLastErrorText(rest)
            CkRest::ckDispose(rest)
            CkOAuth2::ckDispose(oauth2)
            CkStringBuilder::ckDispose(sbErrResponse)
            ProcedureReturn
        EndIf

        Debug "Error response:" + CkStringBuilder::ckGetAsString(sbErrResponse)
        CkRest::ckDispose(rest)
        CkOAuth2::ckDispose(oauth2)
        CkStringBuilder::ckDispose(sbErrResponse)
        ProcedureReturn
    EndIf

    ;  Stream the response body to the output file.
    respBodyStream.i = CkStream::ckCreate()
    If respBodyStream.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStream::setCkSinkFile(respBodyStream, "/someDirectoryPath/test.pdf")
    success = CkRest::ckReadRespBodyStream(rest,respBodyStream,1)
    If success <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkOAuth2::ckDispose(oauth2)
        CkStringBuilder::ckDispose(sbErrResponse)
        CkStream::ckDispose(respBodyStream)
        ProcedureReturn
    EndIf

    Debug "Example Completed."


    CkRest::ckDispose(rest)
    CkOAuth2::ckDispose(oauth2)
    CkStringBuilder::ckDispose(sbErrResponse)
    CkStream::ckDispose(respBodyStream)


    ProcedureReturn
EndProcedure