PureBasic
PureBasic
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 PureBasic Downloads
IncludeFile "CkHttp.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkRest.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkOAuth2.pb"
Procedure ChilkatExample()
success.i = 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
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 = 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