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

C
#include <C_CkOAuth2.h>
#include <C_CkRest.h>
#include <C_CkStringBuilder.h>
#include <C_CkJsonObject.h>
#include <C_CkHttp.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkOAuth2 oauth2;
    HCkRest rest;
    const char *photoId;
    HCkStringBuilder sbPath;
    const char *responseJson;
    HCkJsonObject json;
    const char *imageUrl;
    HCkStringBuilder sbImageUrl;
    HCkStringBuilder sbToPath;
    BOOL bCaseSensitive;
    HCkHttp 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 = CkOAuth2_Create();
    CkOAuth2_putAccessToken(oauth2,"FACEBOOK-ACCESS-TOKEN");

    rest = CkRest_Create();

    // Connect to Facebook...
    success = CkRest_Connect(rest,"graph.facebook.com",443,TRUE,TRUE);
    if (success != TRUE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkOAuth2_Dispose(oauth2);
        CkRest_Dispose(rest);
        return;
    }

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

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

    sbPath = CkStringBuilder_Create();
    CkStringBuilder_Append(sbPath,"/v2.7/");
    CkStringBuilder_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/
    CkRest_AddQueryParam(rest,"fields","id,album,images");

    responseJson = CkRest_fullRequestNoBody(rest,"GET",CkStringBuilder_getAsString(sbPath));
    if (CkRest_getLastMethodSuccess(rest) != TRUE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkOAuth2_Dispose(oauth2);
        CkRest_Dispose(rest);
        CkStringBuilder_Dispose(sbPath);
        return;
    }

    json = CkJsonObject_Create();
    CkJsonObject_putEmitCompact(json,FALSE);
    CkJsonObject_Load(json,responseJson);

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

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

    sbImageUrl = CkStringBuilder_Create();
    CkStringBuilder_Append(sbImageUrl,imageUrl);

    // Build the output local file path.
    sbToPath = CkStringBuilder_Create();
    CkStringBuilder_Append(sbToPath,"qa_output/fb");
    CkStringBuilder_Append(sbToPath,CkJsonObject_stringOf(json,"id"));
    bCaseSensitive = FALSE;
    if (CkStringBuilder_Contains(sbImageUrl,".jpg",bCaseSensitive) == TRUE) {
        CkStringBuilder_Append(sbToPath,".jpg");
    }
    else {
        CkStringBuilder_Append(sbToPath,".png");
    }

    printf("Downloading to %s\n",CkStringBuilder_getAsString(sbToPath));

    // Download using Chilkat HTTP.
    http = CkHttp_Create();
    success = CkHttp_Download(http,imageUrl,CkStringBuilder_getAsString(sbToPath));
    if (success != TRUE) {
        printf("%s\n",CkHttp_lastErrorText(http));
    }
    else {
        printf("Downloaded.\n");
    }



    CkOAuth2_Dispose(oauth2);
    CkRest_Dispose(rest);
    CkStringBuilder_Dispose(sbPath);
    CkJsonObject_Dispose(json);
    CkStringBuilder_Dispose(sbImageUrl);
    CkStringBuilder_Dispose(sbToPath);
    CkHttp_Dispose(http);

    }