Go
Go
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 Go Downloads
success := 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
oauth2 := chilkat.NewOAuth2()
oauth2.SetAccessToken("FACEBOOK-ACCESS-TOKEN")
rest := chilkat.NewRest()
// Connect to Facebook...
success = rest.Connect("graph.facebook.com",443,true,true)
if success != true {
fmt.Println(rest.LastErrorText())
oauth2.DisposeOAuth2()
rest.DisposeRest()
return
}
// Provide the authentication credentials (i.e. the access key)
rest.SetAuthOAuth2(oauth2)
// Assumes we've already obtained a Photo ID.
photoId := "10210199026347451"
sbPath := chilkat.NewStringBuilder()
sbPath.Append("/v2.7/")
sbPath.Append(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/
rest.AddQueryParam("fields","id,album,images")
responseJson := rest.FullRequestNoBody("GET",*sbPath.GetAsString())
if rest.LastMethodSuccess() != true {
fmt.Println(rest.LastErrorText())
oauth2.DisposeOAuth2()
rest.DisposeRest()
sbPath.DisposeStringBuilder()
return
}
json := chilkat.NewJsonObject()
json.SetEmitCompact(false)
json.Load(*responseJson)
// Show the JSON in human-readable format.
fmt.Println(*json.Emit())
// Get the image URL.
imageUrl := json.StringOf("images[0].source")
fmt.Println("Downloading from ", *imageUrl)
sbImageUrl := chilkat.NewStringBuilder()
sbImageUrl.Append(*imageUrl)
// Build the output local file path.
sbToPath := chilkat.NewStringBuilder()
sbToPath.Append("qa_output/fb")
sbToPath.Append(*json.StringOf("id"))
bCaseSensitive := false
if sbImageUrl.Contains(".jpg",bCaseSensitive) == true {
sbToPath.Append(".jpg")
} else {
sbToPath.Append(".png")
}
fmt.Println("Downloading to ", *sbToPath.GetAsString())
// Download using Chilkat HTTP.
http := chilkat.NewHttp()
success = http.Download(*imageUrl,*sbToPath.GetAsString())
if success != true {
fmt.Println(http.LastErrorText())
} else {
fmt.Println("Downloaded.")
}
oauth2.DisposeOAuth2()
rest.DisposeRest()
sbPath.DisposeStringBuilder()
json.DisposeJsonObject()
sbImageUrl.DisposeStringBuilder()
sbToPath.DisposeStringBuilder()
http.DisposeHttp()