Sample code for 30+ languages & platforms
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 C Downloads

C
#include <C_CkAuthGoogle.h>
#include <C_CkRest.h>
#include <C_CkJsonObject.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkAuthGoogle gAuth;
    HCkRest rest;
    BOOL bAutoReconnect;
    const char *jsonResponse;
    HCkJsonObject 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 = CkAuthGoogle_Create();
    CkAuthGoogle_putAccessToken(gAuth,"GOOGLE-DRIVE-ACCESS-TOKEN");

    rest = CkRest_Create();

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

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

    // Get 4 results per page.  (The default page size is 100, with a max of 1000.
    CkRest_AddQueryParam(rest,"pageSize","4");

    // This uses the Google Drive V3 API... (not V2)
    jsonResponse = CkRest_fullRequestNoBody(rest,"GET","/drive/v3/files");
    if (CkRest_getLastMethodSuccess(rest) != TRUE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkAuthGoogle_Dispose(gAuth);
        CkRest_Dispose(rest);
        return;
    }

    // A successful response will have a status code equal to 200.
    if (CkRest_getResponseStatusCode(rest) != 200) {
        printf("request header = %s\n",CkRest_lastRequestHeader(rest));
        printf("response status code = %d\n",CkRest_getResponseStatusCode(rest));
        printf("response status text = %s\n",CkRest_responseStatusText(rest));
        printf("response header: %s\n",CkRest_responseHeader(rest));
        printf("response JSON: %s\n",jsonResponse);
        CkAuthGoogle_Dispose(gAuth);
        CkRest_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 = CkJsonObject_Create();
    CkJsonObject_Load(json,jsonResponse);

    // Show the full JSON response.
    CkJsonObject_putEmitCompact(json,FALSE);
    printf("%s\n",CkJsonObject_emit(json));

    numFiles = CkJsonObject_SizeOfArray(json,"files");
    i = 0;
    while (i < numFiles) {
        CkJsonObject_putI(json,i);
        printf("name: %s\n",CkJsonObject_stringOf(json,"files[i].name"));
        printf("id: %s\n",CkJsonObject_stringOf(json,"files[i].id"));
        printf("mimeType: %s\n",CkJsonObject_stringOf(json,"files[i].mimeType"));
        printf("-\n");
        i = i + 1;
    }



    CkAuthGoogle_Dispose(gAuth);
    CkRest_Dispose(rest);
    CkJsonObject_Dispose(json);

    }