Sample code for 30+ languages & platforms
Xbase++

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oOauth2
LOCAL oRest
LOCAL cPhotoId
LOCAL oSbPath
LOCAL cResponseJson
LOCAL oJson
LOCAL cImageUrl
LOCAL oSbImageUrl
LOCAL oSbToPath
LOCAL nBCaseSensitive
LOCAL oHttp

nSuccess := 0

//  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 := CreateObject("Chilkat.OAuth2")
oOauth2:AccessToken := "FACEBOOK-ACCESS-TOKEN"

oRest := CreateObject("Chilkat.Rest")

//  Connect to Facebook...
nSuccess := oRest:Connect("graph.facebook.com", 443, 1, 1)
IF (nSuccess != 1)
    ? oRest:LastErrorText
    oOauth2:destroy()
    oRest:destroy()
    RETURN
ENDIF

//  Provide the authentication credentials (i.e. the access key)
oRest:SetAuthOAuth2(oOauth2)

//  Assumes we've already obtained a Photo ID.
cPhotoId := "10210199026347451"

oSbPath := CreateObject("Chilkat.StringBuilder")
oSbPath:Append("/v2.7/")
oSbPath:Append(cPhotoId)

//  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")

cResponseJson := oRest:FullRequestNoBody("GET", oSbPath:GetAsString())
IF (oRest:LastMethodSuccess != 1)
    ? oRest:LastErrorText
    oOauth2:destroy()
    oRest:destroy()
    oSbPath:destroy()
    RETURN
ENDIF

oJson := CreateObject("Chilkat.JsonObject")
oJson:EmitCompact := 0
oJson:Load(cResponseJson)

//  Show the JSON in human-readable format.
? oJson:Emit()

//  Get the image URL.
cImageUrl := oJson:StringOf("images[0].source")
? "Downloading from " + cImageUrl

oSbImageUrl := CreateObject("Chilkat.StringBuilder")
oSbImageUrl:Append(cImageUrl)

//  Build the output local file path.
oSbToPath := CreateObject("Chilkat.StringBuilder")
oSbToPath:Append("qa_output/fb")
oSbToPath:Append(oJson:StringOf("id"))
nBCaseSensitive := 0
IF (oSbImageUrl:Contains(".jpg", nBCaseSensitive) == 1)
    oSbToPath:Append(".jpg")
ELSE
    oSbToPath:Append(".png")
ENDIF

? "Downloading to " + oSbToPath:GetAsString()

//  Download using Chilkat HTTP.
oHttp := CreateObject("Chilkat.Http")
nSuccess := oHttp:Download(cImageUrl, oSbToPath:GetAsString())
IF (nSuccess != 1)
    ? oHttp:LastErrorText
ELSE
    ? "Downloaded."
ENDIF

oOauth2:destroy()
oRest:destroy()
oSbPath:destroy()
oJson:destroy()
oSbImageUrl:destroy()
oSbToPath:destroy()
oHttp:destroy()