Lianja
Lianja
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 Lianja Downloads
llSuccess = .F.
// 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
loOauth2 = createobject("CkOAuth2")
loOauth2.AccessToken = "FACEBOOK-ACCESS-TOKEN"
loRest = createobject("CkRest")
// Connect to Facebook...
llSuccess = loRest.Connect("graph.facebook.com",443,.T.,.T.)
if (llSuccess <> .T.) then
? loRest.LastErrorText
release loOauth2
release loRest
return
endif
// Provide the authentication credentials (i.e. the access key)
loRest.SetAuthOAuth2(loOauth2)
// Assumes we've already obtained a Photo ID.
lcPhotoId = "10210199026347451"
loSbPath = createobject("CkStringBuilder")
loSbPath.Append("/v2.7/")
loSbPath.Append(lcPhotoId)
// 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/
loRest.AddQueryParam("fields","id,album,images")
lcResponseJson = loRest.FullRequestNoBody("GET",loSbPath.GetAsString())
if (loRest.LastMethodSuccess <> .T.) then
? loRest.LastErrorText
release loOauth2
release loRest
release loSbPath
return
endif
loJson = createobject("CkJsonObject")
loJson.EmitCompact = .F.
loJson.Load(lcResponseJson)
// Show the JSON in human-readable format.
? loJson.Emit()
// Get the image URL.
lcImageUrl = loJson.StringOf("images[0].source")
? "Downloading from " + lcImageUrl
loSbImageUrl = createobject("CkStringBuilder")
loSbImageUrl.Append(lcImageUrl)
// Build the output local file path.
loSbToPath = createobject("CkStringBuilder")
loSbToPath.Append("qa_output/fb")
loSbToPath.Append(loJson.StringOf("id"))
llBCaseSensitive = .F.
if (loSbImageUrl.Contains(".jpg",llBCaseSensitive) = .T.) then
loSbToPath.Append(".jpg")
else
loSbToPath.Append(".png")
endif
? "Downloading to " + loSbToPath.GetAsString()
// Download using Chilkat HTTP.
loHttp = createobject("CkHttp")
llSuccess = loHttp.Download(lcImageUrl,loSbToPath.GetAsString())
if (llSuccess <> .T.) then
? loHttp.LastErrorText
else
? "Downloaded."
endif
release loOauth2
release loRest
release loSbPath
release loJson
release loSbImageUrl
release loSbToPath
release loHttp