Sample code for 30+ languages & platforms
Unicode C

List Files in Google Drive

See more Google Drive Examples

Demonstrates how to list files in Google Drive.

See 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;
    const wchar_t *jsonResponse;
    HCkJsonObjectW json;
    int numFiles;
    int i;

    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 4 results per page.  (The default page size is 100, with a max of 1000.
    CkRestW_AddQueryParam(rest,L"pageSize",L"4");

    // This uses the Google Drive V3 API... (not V2)
    jsonResponse = CkRestW_fullRequestNoBody(rest,L"GET",L"/drive/v3/files");
    if (CkRestW_getLastMethodSuccess(rest) != TRUE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkAuthGoogleW_Dispose(gAuth);
        CkRestW_Dispose(rest);
        return;
    }

    // A successful response will have a status code equal to 200.
    if (CkRestW_getResponseStatusCode(rest) != 200) {
        wprintf(L"request header = %s\n",CkRestW_lastRequestHeader(rest));
        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);
        return;
    }

    // A successful response looks like this:
    // {
    //  "kind": "drive#fileList",
    //  "files": [
    //    {
    //      "kind": "drive#file",
    //      "id": "0B53Q6OSTWYolenpjTEU4ekJlQUU",
    //      "name": "test",
    //      "mimeType": "application/vnd.google-apps.folder"
    //    },
    //    {
    //      "kind": "drive#file",
    //      "id": "0B53Q6OSTWYolRm4ycjZtdXhRaEE",
    //      "name": "starfish4.jpg",
    //      "mimeType": "image/jpeg"
    //    },
    //    {
    //      "kind": "drive#file",
    //      "id": "0B53Q6OSTWYolMWt2VzN0Qlo1UjA",
    //      "name": "hamlet2.xml",
    //      "mimeType": "text/xml"
    //    },
    // ...
    //    {
    //      "kind": "drive#file",
    //      "id": "0B53Q6OSTWYolc3RhcnRlcl9maWxlX2Rhc2hlclYw",
    //      "name": "Getting started",
    //      "mimeType": "application/pdf"
    //    }
    //  ]
    // }

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

    // Show the full JSON response.
    CkJsonObjectW_putEmitCompact(json,FALSE);
    wprintf(L"%s\n",CkJsonObjectW_emit(json));

    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;
    }



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

    }