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
(Delphi DLL) 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.
uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Http, OAuth2, Rest, StringBuilder, JsonObject; ... procedure TForm1.Button1Click(Sender: TObject); var oauth2: HCkOAuth2; rest: HCkRest; success: Boolean; photoId: PWideChar; sbPath: HCkStringBuilder; responseJson: PWideChar; json: HCkJsonObject; imageUrl: PWideChar; sbImageUrl: HCkStringBuilder; sbToPath: HCkStringBuilder; bCaseSensitive: Boolean; http: HCkHttp; begin // 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) then begin Memo1.Lines.Add(CkRest__lastErrorText(rest)); Exit; end; // 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) then begin Memo1.Lines.Add(CkRest__lastErrorText(rest)); Exit; end; json := CkJsonObject_Create(); CkJsonObject_putEmitCompact(json,False); CkJsonObject_Load(json,responseJson); // Show the JSON in human-readable format. Memo1.Lines.Add(CkJsonObject__emit(json)); // Get the image URL. imageUrl := CkJsonObject__stringOf(json,'images[0].source'); Memo1.Lines.Add('Downloading from ' + 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) then begin CkStringBuilder_Append(sbToPath,'.jpg'); end else begin CkStringBuilder_Append(sbToPath,'.png'); end; Memo1.Lines.Add('Downloading to ' + CkStringBuilder__getAsString(sbToPath)); // Download using Chilkat HTTP. http := CkHttp_Create(); success := CkHttp_Download(http,imageUrl,CkStringBuilder__getAsString(sbToPath)); if (success <> True) then begin Memo1.Lines.Add(CkHttp__lastErrorText(http)); end else begin Memo1.Lines.Add('Downloaded.'); end; CkOAuth2_Dispose(oauth2); CkRest_Dispose(rest); CkStringBuilder_Dispose(sbPath); CkJsonObject_Dispose(json); CkStringBuilder_Dispose(sbImageUrl); CkStringBuilder_Dispose(sbToPath); CkHttp_Dispose(http); end; |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.