Unicode 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_CkRestW.h>
#include <C_CkOAuth2W.h>
#include <C_CkStringBuilderW.h>
#include <C_CkStreamW.h>

void ChilkatSample(void)
    {
    HCkRestW rest;
    BOOL success;
    HCkOAuth2W oauth2;
    int statusCode;
    HCkStringBuilderW sbErrResponse;
    HCkStreamW respBodyStream;

    rest = CkRestW_Create();

    //   Provide a previously obtained OAuth2 access token.
    oauth2 = CkOAuth2W_Create();
    CkOAuth2W_putAccessToken(oauth2,L"OAUTH2_ACCESS_TOKEN");
    CkRestW_SetAuthOAuth2(rest,oauth2);

    success = CkRestW_Connect(rest,L"www.googleapis.com",443,TRUE,TRUE);
    if (success != TRUE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        CkOAuth2W_Dispose(oauth2);
        return;
    }

    CkRestW_AddQueryParam(rest,L"includeTeamDriveItems",L"true");
    CkRestW_AddQueryParam(rest,L"supportsTeamDrives",L"true");
    CkRestW_AddQueryParam(rest,L"alt",L"media");

    //  First send the HTTP request.
    success = CkRestW_SendReqNoBody(rest,L"GET",L"/drive/v3/files/1R_70heIyzIAu1_u0prXbYcaIiJRVkgBl");
    if (success != TRUE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        CkOAuth2W_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 = CkRestW_ReadResponseHeader(rest);
    if (statusCode < 0) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        CkOAuth2W_Dispose(oauth2);
        return;
    }

    if (statusCode != 200) {
        wprintf(L"Received error response code: %d\n",statusCode);
        //  Read the error response body.
        sbErrResponse = CkStringBuilderW_Create();
        success = CkRestW_ReadRespSb(rest,sbErrResponse);
        if (success != TRUE) {
            wprintf(L"%s\n",CkRestW_lastErrorText(rest));
            CkRestW_Dispose(rest);
            CkOAuth2W_Dispose(oauth2);
            CkStringBuilderW_Dispose(sbErrResponse);
            return;
        }

        wprintf(L"Error response:%s\n",CkStringBuilderW_getAsString(sbErrResponse));
        CkRestW_Dispose(rest);
        CkOAuth2W_Dispose(oauth2);
        CkStringBuilderW_Dispose(sbErrResponse);
        return;
    }

    //  Stream the response body to the output file.
    respBodyStream = CkStreamW_Create();
    CkStreamW_putSinkFile(respBodyStream,L"/someDirectory/penguins.jpg");
    success = CkRestW_ReadRespBodyStream(rest,respBodyStream,TRUE);
    if (success != TRUE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        CkOAuth2W_Dispose(oauth2);
        CkStringBuilderW_Dispose(sbErrResponse);
        CkStreamW_Dispose(respBodyStream);
        return;
    }

    wprintf(L"Example Completed.\n");


    CkRestW_Dispose(rest);
    CkOAuth2W_Dispose(oauth2);
    CkStringBuilderW_Dispose(sbErrResponse);
    CkStreamW_Dispose(respBodyStream);

    }