Sample code for 30+ languages & platforms
Unicode C

Download Photo to a File

See more Facebook Examples

Assuming we have the ID of a Photo, this example demonstrates how to download the photo image data to a file.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkOAuth2W.h>
#include <C_CkRestW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkOAuth2W oauth2;
    HCkRestW rest;
    const wchar_t *photoId;
    HCkStringBuilderW sbPath;
    const wchar_t *responseJson;
    HCkJsonObjectW json;
    const wchar_t *imageUrl;
    HCkStringBuilderW sbImageUrl;
    HCkStringBuilderW sbToPath;
    BOOL bCaseSensitive;
    HCkHttpW http;

    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 != TRUE) {
        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);

    // Assumes we've already obtained a Photo ID.
    photoId = L"10210199026347451";

    sbPath = CkStringBuilderW_Create();
    CkStringBuilderW_Append(sbPath,L"/v2.7/");
    CkStringBuilderW_Append(sbPath,photoId);

    // First we're going to get the photo informaton so we can get the URL of the image file data.
    // Select the fields we want.
    // See https://developers.facebook.com/docs/graph-api/reference/photo/
    CkRestW_AddQueryParam(rest,L"fields",L"id,album,images");

    responseJson = CkRestW_fullRequestNoBody(rest,L"GET",CkStringBuilderW_getAsString(sbPath));
    if (CkRestW_getLastMethodSuccess(rest) != TRUE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkOAuth2W_Dispose(oauth2);
        CkRestW_Dispose(rest);
        CkStringBuilderW_Dispose(sbPath);
        return;
    }

    json = CkJsonObjectW_Create();
    CkJsonObjectW_putEmitCompact(json,FALSE);
    CkJsonObjectW_Load(json,responseJson);

    // Show the JSON in human-readable format.
    wprintf(L"%s\n",CkJsonObjectW_emit(json));

    // Get the image URL.
    imageUrl = CkJsonObjectW_stringOf(json,L"images[0].source");
    wprintf(L"Downloading from %s\n",imageUrl);

    sbImageUrl = CkStringBuilderW_Create();
    CkStringBuilderW_Append(sbImageUrl,imageUrl);

    // Build the output local file path.
    sbToPath = CkStringBuilderW_Create();
    CkStringBuilderW_Append(sbToPath,L"qa_output/fb");
    CkStringBuilderW_Append(sbToPath,CkJsonObjectW_stringOf(json,L"id"));
    bCaseSensitive = FALSE;
    if (CkStringBuilderW_Contains(sbImageUrl,L".jpg",bCaseSensitive) == TRUE) {
        CkStringBuilderW_Append(sbToPath,L".jpg");
    }
    else {
        CkStringBuilderW_Append(sbToPath,L".png");
    }

    wprintf(L"Downloading to %s\n",CkStringBuilderW_getAsString(sbToPath));

    // Download using Chilkat HTTP.
    http = CkHttpW_Create();
    success = CkHttpW_Download(http,imageUrl,CkStringBuilderW_getAsString(sbToPath));
    if (success != TRUE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
    }
    else {
        wprintf(L"Downloaded.\n");
    }



    CkOAuth2W_Dispose(oauth2);
    CkRestW_Dispose(rest);
    CkStringBuilderW_Dispose(sbPath);
    CkJsonObjectW_Dispose(json);
    CkStringBuilderW_Dispose(sbImageUrl);
    CkStringBuilderW_Dispose(sbToPath);
    CkHttpW_Dispose(http);

    }