Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(C) Download Photo to a FileAssuming we have the ID of a Photo, this example demonstrates how to download the photo image data to a file.
#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-2024 Chilkat Software, Inc. All Rights Reserved.