C Google Drive: Download a File (Stream to the Filesystem)

Back to Index

Downloads the content of a file by ID. (In this example the file id = 1R_70heIyzIAu1_u0prXbYcaIiJRVkgBl) The file is streamed directly to the filesystem. Note: The alt=media query param must be used to download the file content (as opposed to the file metadata).

Documentation: https://developers.google.com/drive/v3/reference/files/get


#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,"includeTeamDriveItems","true");
    CkRest_AddQueryParam(rest,"supportsTeamDrives","true");
    CkRest_AddQueryParam(rest,"alt","media");

    //  First send the HTTP request.
    success = CkRest_SendReqNoBody(rest,"GET","/drive/v3/files/1R_70heIyzIAu1_u0prXbYcaIiJRVkgBl");
    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,"/someDirectory/penguins.jpg");
    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);

    }