Sample code for 30+ languages & platforms
Unicode C

Search for Files in Google Drive

See more Google Drive Examples

This example follows the same methodology for listing all files in Google Drive in pages, but applies a search filter. It shows how to apply a query parameter for filtering the file results. See the Google Drive Files list for more optional HTTP parameters.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkAuthGoogleW.h>
#include <C_CkRestW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkAuthGoogleW gAuth;
    HCkRestW rest;
    BOOL bAutoReconnect;
    HCkJsonObjectW json;
    int i;
    int numFiles;
    const wchar_t *jsonResponse;
    int pageNumber;
    const wchar_t *pageToken;
    BOOL bContinueLoop;
    BOOL bHasMorePages;

    success = FALSE;

    success = TRUE;

    // It requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // This example uses a previously obtained access token having permission for the 
    // Google Drive scope.

    gAuth = CkAuthGoogleW_Create();
    CkAuthGoogleW_putAccessToken(gAuth,L"GOOGLE-DRIVE-ACCESS-TOKEN");

    rest = CkRestW_Create();

    // Connect using TLS.
    bAutoReconnect = TRUE;
    success = CkRestW_Connect(rest,L"www.googleapis.com",443,TRUE,bAutoReconnect);

    // Provide the authentication credentials (i.e. the access token)
    CkRestW_SetAuthGoogle(rest,gAuth);

    // Get 5 results per page for testing.  (The default page size is 100, with a max of 1000.
    CkRestW_AddQueryParam(rest,L"pageSize",L"5");

    // Our search filter is to list all files containing ".jpg" (i.e. all JPG image files)
    CkRestW_AddQueryParam(rest,L"q",L"name contains '.jpg'");

    json = CkJsonObjectW_Create();

    // Send the request for the 1st page.
    jsonResponse = CkRestW_fullRequestNoBody(rest,L"GET",L"/drive/v3/files");

    pageNumber = 1;

    bContinueLoop = CkRestW_getLastMethodSuccess(rest) && (CkRestW_getResponseStatusCode(rest) == 200);

    while (bContinueLoop == TRUE) {

        wprintf(L"---- Page %d ----\n",pageNumber);

        // Iterate over each file in the response and show the name, id, and mimeType.
        CkJsonObjectW_Load(json,jsonResponse);

        numFiles = CkJsonObjectW_SizeOfArray(json,L"files");
        i = 0;
        while (i < numFiles) {
            CkJsonObjectW_putI(json,i);
            wprintf(L"name: %s\n",CkJsonObjectW_stringOf(json,L"files[i].name"));
            wprintf(L"id: %s\n",CkJsonObjectW_stringOf(json,L"files[i].id"));
            wprintf(L"mimeType: %s\n",CkJsonObjectW_stringOf(json,L"files[i].mimeType"));
            wprintf(L"-\n");
            i = i + 1;
        }

        // Get the next page of files.
        // If the "nextPageToken" is present in the JSON response, then use it in the "pageToken" parameter
        // for the next request.   If no "nextPageToken" was present, then this was the last page of files.
        pageToken = CkJsonObjectW_stringOf(json,L"nextPageToken");
        bContinueLoop = FALSE;
        bHasMorePages = CkJsonObjectW_getLastMethodSuccess(json);
        if (bHasMorePages == TRUE) {
            CkRestW_ClearAllQueryParams(rest);
            CkRestW_AddQueryParam(rest,L"pageSize",L"5");
            CkRestW_AddQueryParam(rest,L"pageToken",pageToken);
            CkRestW_AddQueryParam(rest,L"q",L"name contains '.jpg'");

            jsonResponse = CkRestW_fullRequestNoBody(rest,L"GET",L"/drive/v3/files");
            bContinueLoop = CkRestW_getLastMethodSuccess(rest) && (CkRestW_getResponseStatusCode(rest) == 200);
            pageNumber = pageNumber + 1;
        }

    }

    if (CkRestW_getLastMethodSuccess(rest) != TRUE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkAuthGoogleW_Dispose(gAuth);
        CkRestW_Dispose(rest);
        CkJsonObjectW_Dispose(json);
        return;
    }

    // A successful response will have a status code equal to 200.
    if (CkRestW_getResponseStatusCode(rest) != 200) {
        wprintf(L"response status code = %d\n",CkRestW_getResponseStatusCode(rest));
        wprintf(L"response status text = %s\n",CkRestW_responseStatusText(rest));
        wprintf(L"response header: %s\n",CkRestW_responseHeader(rest));
        wprintf(L"response JSON: %s\n",jsonResponse);
        CkAuthGoogleW_Dispose(gAuth);
        CkRestW_Dispose(rest);
        CkJsonObjectW_Dispose(json);
        return;
    }



    CkAuthGoogleW_Dispose(gAuth);
    CkRestW_Dispose(rest);
    CkJsonObjectW_Dispose(json);

    }