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
(Unicode 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_CkOAuth2W.h> #include <C_CkRestW.h> #include <C_CkStringBuilderW.h> #include <C_CkJsonObjectW.h> #include <C_CkHttpW.h> void ChilkatSample(void) { HCkOAuth2W oauth2; HCkRestW rest; BOOL success; const wchar_t *photoId; HCkStringBuilderW sbPath; const wchar_t *responseJson; HCkJsonObjectW json; const wchar_t *imageUrl; HCkStringBuilderW sbImageUrl; HCkStringBuilderW sbToPath; BOOL bCaseSensitive; HCkHttpW 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 = 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); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.