PureBasic
PureBasic
Iterate Pages in Feed
See more Facebook Examples
Demonstrates how to read the next page in the user's Facebook feed, and iterates through all of the pages in the Facebook feed.Chilkat PureBasic Downloads
IncludeFile "CkUrl.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 and send the following GET request:
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)
; Gets the 1st page in the user's feed.
responseJson.s = CkRest::ckFullRequestNoBody(rest,"GET","/v2.7/me/feed")
If CkRest::ckLastMethodSuccess(rest) <> 1
Debug CkRest::ckLastErrorText(rest)
CkOAuth2::ckDispose(oauth2)
CkRest::ckDispose(rest)
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)
;
; See Parsing the Facebook User Feed for code showing how to parse the JSON feed content.
;
nextUrl.i = CkUrl::ckCreate()
If nextUrl.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Get the URL for the next page in the feed.
nextUrlStr.s = CkJsonObject::ckStringOf(json,"paging.next")
While CkJsonObject::ckLastMethodSuccess(json) = 1
Debug "Next page URL: " + nextUrlStr
CkUrl::ckParseUrl(nextUrl,nextUrlStr)
; Prepare for getting the next page in the feed.
; We can continue using the same REST object.
; If already connected, we'll continue using the existing connection.
; Otherwise, a new connection will automatically be made if needed.
CkRest::ckClearAllQueryParams(rest)
CkRest::ckAddQueryParams(rest,CkUrl::ckQuery(nextUrl))
responseJson = CkRest::ckFullRequestNoBody(rest,"GET","/v2.7/me/feed")
If CkRest::ckLastMethodSuccess(rest) <> 1
Debug CkRest::ckLastErrorText(rest)
CkOAuth2::ckDispose(oauth2)
CkRest::ckDispose(rest)
CkJsonObject::ckDispose(json)
CkUrl::ckDispose(nextUrl)
ProcedureReturn
EndIf
CkJsonObject::ckLoad(json,responseJson)
; See Parsing the Facebook User Feed for code showing how to parse the JSON feed content.
; Get the URL for the next page.
nextUrlStr = CkJsonObject::ckStringOf(json,"paging.next")
Wend
Debug "No more pages in the feed."
CkOAuth2::ckDispose(oauth2)
CkRest::ckDispose(rest)
CkJsonObject::ckDispose(json)
CkUrl::ckDispose(nextUrl)
ProcedureReturn
EndProcedure