Sample code for 30+ languages & platforms
Unicode C

Get the Photos for a User

See more Facebook Examples

Demonstrates how to get the photos that the user has uploaded.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkOAuth2W.h>
#include <C_CkRestW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkDateTimeW.h>
#include <C_CkDtObjW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkOAuth2W oauth2;
    HCkRestW rest;
    const wchar_t *responseJson;
    HCkJsonObjectW json;
    HCkDateTimeW dtime;
    BOOL bLocalTime;
    HCkDtObjW dt;
    int i;
    int numItems;
    const wchar_t *name;

    success = FALSE;

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

    //  This example assumes a previously obtained an access token
    oauth2 = CkOAuth2W_Create();
    CkOAuth2W_putAccessToken(oauth2,L"FACEBOOK-ACCESS-TOKEN");

    rest = CkRestW_Create();

    //  Connect to Facebook.
    success = CkRestW_Connect(rest,L"graph.facebook.com",443,TRUE,TRUE);
    if (success == FALSE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkOAuth2W_Dispose(oauth2);
        CkRestW_Dispose(rest);
        return;
    }

    //  Provide the authentication credentials (i.e. the access key)
    CkRestW_SetAuthOAuth2(rest,oauth2);

    //  Indicate that we only want the photos the user has personally uploaded.
    CkRestW_AddQueryParam(rest,L"type",L"uploaded");

    //  We could limit the number of photos by setting a limit.
    CkRestW_AddQueryParam(rest,L"limit",L"5");

    //  Gets the 1st page of photos. (Not the actual image data, but the information about each photo.)
    //  See https://developers.facebook.com/docs/graph-api/reference/user/photos/ for more information.
    responseJson = CkRestW_fullRequestNoBody(rest,L"GET",L"/v2.7/me/photos");
    if (CkRestW_getLastMethodSuccess(rest) == FALSE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkOAuth2W_Dispose(oauth2);
        CkRestW_Dispose(rest);
        return;
    }

    json = CkJsonObjectW_Create();
    CkJsonObjectW_putEmitCompact(json,FALSE);
    CkJsonObjectW_Load(json,responseJson);
    wprintf(L"%s\n",CkJsonObjectW_emit(json));

    //  A sample JSON response is shown below.  
    //  This is the code to parse the JSON response.

    dtime = CkDateTimeW_Create();
    bLocalTime = TRUE;

    dt = CkDtObjW_Create();
    i = 0;
    numItems = CkJsonObjectW_SizeOfArray(json,L"data");
    while (i < numItems) {
        CkJsonObjectW_putI(json,i);
        wprintf(L"--- %d\n",i);
        name = CkJsonObjectW_stringOf(json,L"data[i].name");
        if (CkJsonObjectW_getLastMethodSuccess(json) == TRUE) {
            wprintf(L"name: %s\n",name);
        }

        wprintf(L"id: %s\n",CkJsonObjectW_stringOf(json,L"data[i].id"));

        //  We can load the created_time into a CkDateTime...
        CkDateTimeW_SetFromTimestamp(dtime,CkJsonObjectW_stringOf(json,L"data[i].created_time"));
        CkDateTimeW_ToDtObj(dtime,bLocalTime,dt);

        wprintf(L"%d/%d/%d  %d:%d\n",CkDtObjW_getMonth(dt),CkDtObjW_getDay(dt),CkDtObjW_getYear(dt),CkDtObjW_getHour(dt),CkDtObjW_getMinute(dt));
        i = i + 1;
    }

    //  We can get the paging information as follows:
    wprintf(L"URL for next page: %s\n",CkJsonObjectW_stringOf(json,L"paging.next"));
    wprintf(L"before cursor: %s\n",CkJsonObjectW_stringOf(json,L"paging.cursors.before"));
    wprintf(L"after cursor: %s\n",CkJsonObjectW_stringOf(json,L"paging.cursors.after"));

    //  This is a sample JSON response:
    //  { 
    //    "data": [
    //      {
    //        "created_time": "2016-09-29T20:46:18+0000",
    //        "name": "Ignore my posts -- I'm doing some testing for Facebook related programming...",
    //        "id": "10210199026347451"
    //      },
    //      { 
    //        "created_time": "2016-09-19T02:00:42+0000",
    //        "id": "10210091531240138"
    //      },
    //      { 
    //        "created_time": "2016-09-19T02:00:42+0000",
    //        "id": "10210091520620125"
    //      },
    //      { 
    //        "created_time": "2016-09-19T01:59:46+0000",
    //        "name": "I would've went for a swim had it not been for the sign",
    //        "id": "10210091522299917"
    //      },
    //      { 
    //        "created_time": "2016-09-12T00:37:35+0000",
    //        "id": "10210023316834798"
    //      }
    //    ],
    //    "paging": { 
    //      "cursors": { 
    //        "before": "MTAyMTAxOTkwMjYzNDc0NTEZD",
    //        "after": "MTAyMTAwMjMzMTU4MzQ3OTgZD"
    //      },
    //      "next": "https:\/\/graph.facebook.com\/v2.7\/10224048320139890\/photos?type=uploaded&limit=5&after=MTAyMTAwMjMzMTU4MzQ3OTgZD"
    //    }
    //  }
    //  


    CkOAuth2W_Dispose(oauth2);
    CkRestW_Dispose(rest);
    CkJsonObjectW_Dispose(json);
    CkDateTimeW_Dispose(dtime);
    CkDtObjW_Dispose(dt);

    }