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
(PureBasic) 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.
IncludeFile "CkHttp.pb" IncludeFile "CkStringBuilder.pb" IncludeFile "CkRest.pb" IncludeFile "CkJsonObject.pb" IncludeFile "CkOAuth2.pb" Procedure ChilkatExample() ; 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.i = CkOAuth2::ckCreate() If oauth2.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkOAuth2::setCkAccessToken(oauth2, "FACEBOOK-ACCESS-TOKEN") rest.i = CkRest::ckCreate() If rest.i = 0 Debug "Failed to create object." ProcedureReturn EndIf ; Connect to Facebook... success.i = CkRest::ckConnect(rest,"graph.facebook.com",443,1,1) If success <> 1 Debug CkRest::ckLastErrorText(rest) CkOAuth2::ckDispose(oauth2) CkRest::ckDispose(rest) ProcedureReturn EndIf ; Provide the authentication credentials (i.e. the access key) CkRest::ckSetAuthOAuth2(rest,oauth2) ; Assumes we've already obtained a Photo ID. photoId.s = "10210199026347451" sbPath.i = CkStringBuilder::ckCreate() If sbPath.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkStringBuilder::ckAppend(sbPath,"/v2.7/") CkStringBuilder::ckAppend(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::ckAddQueryParam(rest,"fields","id,album,images") responseJson.s = CkRest::ckFullRequestNoBody(rest,"GET",CkStringBuilder::ckGetAsString(sbPath)) If CkRest::ckLastMethodSuccess(rest) <> 1 Debug CkRest::ckLastErrorText(rest) CkOAuth2::ckDispose(oauth2) CkRest::ckDispose(rest) CkStringBuilder::ckDispose(sbPath) ProcedureReturn EndIf json.i = CkJsonObject::ckCreate() If json.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkJsonObject::setCkEmitCompact(json, 0) CkJsonObject::ckLoad(json,responseJson) ; Show the JSON in human-readable format. Debug CkJsonObject::ckEmit(json) ; Get the image URL. imageUrl.s = CkJsonObject::ckStringOf(json,"images[0].source") Debug "Downloading from " + imageUrl sbImageUrl.i = CkStringBuilder::ckCreate() If sbImageUrl.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkStringBuilder::ckAppend(sbImageUrl,imageUrl) ; Build the output local file path. sbToPath.i = CkStringBuilder::ckCreate() If sbToPath.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkStringBuilder::ckAppend(sbToPath,"qa_output/fb") CkStringBuilder::ckAppend(sbToPath,CkJsonObject::ckStringOf(json,"id")) bCaseSensitive.i = 0 If CkStringBuilder::ckContains(sbImageUrl,".jpg",bCaseSensitive) = 1 CkStringBuilder::ckAppend(sbToPath,".jpg") Else CkStringBuilder::ckAppend(sbToPath,".png") EndIf Debug "Downloading to " + CkStringBuilder::ckGetAsString(sbToPath) ; Download using Chilkat HTTP. http.i = CkHttp::ckCreate() If http.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success = CkHttp::ckDownload(http,imageUrl,CkStringBuilder::ckGetAsString(sbToPath)) If success <> 1 Debug CkHttp::ckLastErrorText(http) Else Debug "Downloaded." EndIf CkOAuth2::ckDispose(oauth2) CkRest::ckDispose(rest) CkStringBuilder::ckDispose(sbPath) CkJsonObject::ckDispose(json) CkStringBuilder::ckDispose(sbImageUrl) CkStringBuilder::ckDispose(sbToPath) CkHttp::ckDispose(http) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.