Sample code for 30+ languages & platforms
AutoIt

Download Photo to a File

See more Facebook Examples

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

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

; 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
$oOauth2 = ObjCreate("Chilkat.OAuth2")
$oOauth2.AccessToken = "FACEBOOK-ACCESS-TOKEN"

$oRest = ObjCreate("Chilkat.Rest")

; Connect to Facebook...
$bSuccess = $oRest.Connect("graph.facebook.com",443,True,True)
If ($bSuccess <> True) Then
    ConsoleWrite($oRest.LastErrorText & @CRLF)
    Exit
EndIf

; Provide the authentication credentials (i.e. the access key)
$oRest.SetAuthOAuth2($oOauth2)

; Assumes we've already obtained a Photo ID.
Local $sPhotoId = "10210199026347451"

$oSbPath = ObjCreate("Chilkat.StringBuilder")
$oSbPath.Append("/v2.7/")
$oSbPath.Append($sPhotoId)

; 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/
$oRest.AddQueryParam("fields","id,album,images")

Local $sResponseJson = $oRest.FullRequestNoBody("GET",$oSbPath.GetAsString())
If ($oRest.LastMethodSuccess <> True) Then
    ConsoleWrite($oRest.LastErrorText & @CRLF)
    Exit
EndIf

$oJson = ObjCreate("Chilkat.JsonObject")
$oJson.EmitCompact = False
$oJson.Load($sResponseJson)

; Show the JSON in human-readable format.
ConsoleWrite($oJson.Emit() & @CRLF)

; Get the image URL.
Local $sImageUrl = $oJson.StringOf("images[0].source")
ConsoleWrite("Downloading from " & $sImageUrl & @CRLF)

$oSbImageUrl = ObjCreate("Chilkat.StringBuilder")
$oSbImageUrl.Append($sImageUrl)

; Build the output local file path.
$oSbToPath = ObjCreate("Chilkat.StringBuilder")
$oSbToPath.Append("qa_output/fb")
$oSbToPath.Append($oJson.StringOf("id"))
Local $bCaseSensitive = False
If ($oSbImageUrl.Contains(".jpg",$bCaseSensitive) = True) Then
    $oSbToPath.Append(".jpg")
Else
    $oSbToPath.Append(".png")
EndIf

ConsoleWrite("Downloading to " & $oSbToPath.GetAsString() & @CRLF)

; Download using Chilkat HTTP.
$oHttp = ObjCreate("Chilkat.Http")
$bSuccess = $oHttp.Download($sImageUrl,$oSbToPath.GetAsString())
If ($bSuccess <> True) Then
    ConsoleWrite($oHttp.LastErrorText & @CRLF)
Else
    ConsoleWrite("Downloaded." & @CRLF)
EndIf