Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

C Web API Examples

Primary Categories

AWS Translate
Activix CRM
Adyen
Amazon MWS
Azure Maps
Azure Monitor
Azure OAuth2
Azure Storage Accounts
Bluzone
CardConnect
ClickBank
Clickatell
Cloudfare
DocuSign
ETrade
Facebook
GeoOp
GetHarvest
Jira

MYOB
Magento
Microsoft Calendar
Microsoft Group
Microsoft Tasks and Plans
OneLogin OIDC
PayPal
Peoplevox
Populi
QuickBooks
SOAP finkok.com
Shopify
Stripe
SugarCRM
Trello
Twitter
VoiceBase
Vonage
Walmart
Xero
eBay
effectconnect

 

 

 

(C) Download Photo to a File

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

Chilkat C/C++ Library Downloads

MS Visual C/C++

Linux/CentOS C/C++

Alpine Linux C/C++

MAC OS X C/C++

armhf/aarch64 C/C++

C++ Builder

iOS C/C++

Android C/C++

Win Mobile 5.0/Pocket PC 2003

Solaris C/C++

FreeBSD C/C++

OpenBSD C/C++

MinGW C/C++

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

void ChilkatSample(void)
    {
    HCkOAuth2 oauth2;
    HCkRest rest;
    BOOL success;
    const char *photoId;
    HCkStringBuilder sbPath;
    const char *responseJson;
    HCkJsonObject json;
    const char *imageUrl;
    HCkStringBuilder sbImageUrl;
    HCkStringBuilder sbToPath;
    BOOL bCaseSensitive;
    HCkHttp http;

    // 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);

    }

 

© 2000-2019 Chilkat Software, Inc. All Rights Reserved.