C 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


#include <C_CkRest.h>
#include <C_CkOAuth2.h>
#include <C_CkStringBuilder.h>
#include <C_CkStream.h>

void ChilkatSample(void)
    {
    HCkRest rest;
    BOOL success;
    HCkOAuth2 oauth2;
    int statusCode;
    HCkStringBuilder sbErrResponse;
    HCkStream respBodyStream;

    rest = CkRest_Create();

    //   Provide a previously obtained OAuth2 access token.
    oauth2 = CkOAuth2_Create();
    CkOAuth2_putAccessToken(oauth2,"OAUTH2_ACCESS_TOKEN");
    CkRest_SetAuthOAuth2(rest,oauth2);

    success = CkRest_Connect(rest,"www.googleapis.com",443,TRUE,TRUE);
    if (success != TRUE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_Dispose(rest);
        CkOAuth2_Dispose(oauth2);
        return;
    }

    CkRest_AddQueryParam(rest,"mimeType","application/pdf");

    //  First send the HTTP request.
    success = CkRest_SendReqNoBody(rest,"GET","/drive/v3/files/1zvzpTHO1dM9vNKYxpmr4YrX7x_R6qiNGFb8WB8h9wuI/export");
    if (success != TRUE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_Dispose(rest);
        CkOAuth2_Dispose(oauth2);
        return;
    }

    //  Read the response header.  If the response status code is success, stream to the file.
    //  Otherwise receive the error response text.
    statusCode = CkRest_ReadResponseHeader(rest);
    if (statusCode < 0) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_Dispose(rest);
        CkOAuth2_Dispose(oauth2);
        return;
    }

    if (statusCode != 200) {
        printf("Received error response code: %d\n",statusCode);
        //  Read the error response body.
        sbErrResponse = CkStringBuilder_Create();
        success = CkRest_ReadRespSb(rest,sbErrResponse);
        if (success != TRUE) {
            printf("%s\n",CkRest_lastErrorText(rest));
            CkRest_Dispose(rest);
            CkOAuth2_Dispose(oauth2);
            CkStringBuilder_Dispose(sbErrResponse);
            return;
        }

        printf("Error response:%s\n",CkStringBuilder_getAsString(sbErrResponse));
        CkRest_Dispose(rest);
        CkOAuth2_Dispose(oauth2);
        CkStringBuilder_Dispose(sbErrResponse);
        return;
    }

    //  Stream the response body to the output file.
    respBodyStream = CkStream_Create();
    CkStream_putSinkFile(respBodyStream,"/someDirectoryPath/test.pdf");
    success = CkRest_ReadRespBodyStream(rest,respBodyStream,TRUE);
    if (success != TRUE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_Dispose(rest);
        CkOAuth2_Dispose(oauth2);
        CkStringBuilder_Dispose(sbErrResponse);
        CkStream_Dispose(respBodyStream);
        return;
    }

    printf("Example Completed.\n");


    CkRest_Dispose(rest);
    CkOAuth2_Dispose(oauth2);
    CkStringBuilder_Dispose(sbErrResponse);
    CkStream_Dispose(respBodyStream);

    }