PowerBuilder
PowerBuilder
Twitter - Get Follower IDs
Demonstrates how to get a list of follower IDs for a Twitter user. If the number of followers is more than can be returned in a single response, this example will use cursors to page through the followers.This example is deprecated and no longer valid.
Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Json
oleobject loo_Rest
oleobject loo_Oauth1
integer li_BAutoReconnect
oleobject loo_JsonResponse
oleobject loo_SbNextCursor
integer li_PageNum
integer li_CaseSensitive
integer li_BContinue
string ls_Resp
integer li_NumIds
integer i
li_Success = 0
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// ----------------------------------------------------------------------
// This initial setup, which involves setting the OAuth1 properties and connecting
// to api.twitter.com, is only required once at the beginning. Once connected, the same
// object instance may be re-used, and if necessary, it will automatically reconnect
// as needed.
// Assume we've previously obtained an access token and saved it to a JSON file..
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")
if li_rc < 0 then
destroy loo_Json
MessageBox("Error","Connecting to COM object failed")
return
end if
li_Success = loo_Json.LoadFile("qa_data/tokens/twitter.json")
loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat.Rest")
loo_Oauth1 = create oleobject
li_rc = loo_Oauth1.ConnectToNewObject("Chilkat.OAuth1")
loo_Oauth1.ConsumerKey = "TWITTER_CONSUMER_KEY"
loo_Oauth1.ConsumerSecret = "TWITTER_CONSUMER_SECRET"
loo_Oauth1.Token = loo_Json.StringOf("oauth_token")
loo_Oauth1.TokenSecret = loo_Json.StringOf("oauth_token_secret")
loo_Oauth1.SignatureMethod = "HMAC-SHA1"
loo_Oauth1.GenNonce(16)
loo_Rest.SetAuthOAuth1(loo_Oauth1,0)
li_BAutoReconnect = 1
li_Success = loo_Rest.Connect("api.twitter.com",443,1,li_BAutoReconnect)
if li_Success <> 1 then
Write-Debug loo_Rest.LastErrorText
destroy loo_Json
destroy loo_Rest
destroy loo_Oauth1
return
end if
// This ends the initial setup...
// ----------------------------------------------------------------------
// This Twitter user has about 77.5K followers..
loo_Rest.AddQueryParam("screen_name","MarcusMiller959")
loo_JsonResponse = create oleobject
li_rc = loo_JsonResponse.ConnectToNewObject("Chilkat.JsonObject")
loo_JsonResponse.EmitCompact = 0
// Get the 1st page of results using a cursor of "-1".
loo_SbNextCursor = create oleobject
li_rc = loo_SbNextCursor.ConnectToNewObject("Chilkat.StringBuilder")
loo_SbNextCursor.SetString("-1")
li_PageNum = 1
li_CaseSensitive = 0
li_BContinue = 1
// Get a maximum of 5 pages (of 5000 ids each).
do while li_BContinue = 1
if loo_SbNextCursor.ContentsEqual("0",li_CaseSensitive) = 1 then
// This will cause the loop to exit..
li_BContinue = 0
else
// Adds or replaces the query param.
loo_Rest.AddQueryParam("cursor",loo_SbNextCursor.GetAsString())
// Get the next page of follower IDs
ls_Resp = loo_Rest.FullRequestNoBody("GET","/1.1/followers/ids.json")
if loo_Rest.LastMethodSuccess <> 1 then
Write-Debug loo_Rest.LastErrorText
destroy loo_Json
destroy loo_Rest
destroy loo_Oauth1
destroy loo_JsonResponse
destroy loo_SbNextCursor
return
end if
loo_JsonResponse.Load(ls_Resp)
if loo_Rest.ResponseStatusCode <> 200 then
Write-Debug loo_JsonResponse.Emit()
destroy loo_Json
destroy loo_Rest
destroy loo_Oauth1
destroy loo_JsonResponse
destroy loo_SbNextCursor
return
end if
// Show the number of IDs returned in this
li_NumIds = loo_JsonResponse.SizeOfArray("ids")
Write-Debug "Page " + string(li_PageNum) + ", Number of Ids: " + string(li_NumIds)
// Show the 1st 10 ids in this page.
i = 0
do while i < li_NumIds
loo_JsonResponse.I = i
Write-Debug " " + string(i) + ": " + loo_JsonResponse.StringOf("ids[i]")
i = i + 1
if i > 10 then
// Force the loop to exit.
i = li_NumIds
end if
loop
li_PageNum = li_PageNum + 1
if li_PageNum > 5 then
li_BContinue = 0
else
loo_SbNextCursor.SetString(loo_JsonResponse.StringOf("next_cursor_str"))
end if
end if
loop
// A successful JSON response for the 1st page looks like this:
// {
// "ids": [
// 3140496044,
// 793204773751324672,
// 789951187781050369,
// 763520773587922945,
// ...
// 15031286,
// 2668251246,
// 3751659443
// ],
// "next_cursor": 1531680438812851153,
// "next_cursor_str": "1531680438812851153",
// "previous_cursor": 0,
// "previous_cursor_str": "0"
// }
// The output of this program looks like this:
// Page 1, Number of Ids: 5000
// 0: 931953350
// 1: 786708055
// 2: 560845700
// 3: 3140496044
// 4: 793204773751324672
// 5: 789951187781050369
// 6: 763520773587922945
// 7: 793143274059988992
// 8: 793139683412762624
// 9: 1588222783
// 10: 703821370778451968
// Page 2, Number of Ids: 5000
// 0: 15031286
// 1: 2668251246
// 2: 3751659443
// 3: 3324584493
// 4: 2440214809
// 5: 1322335441
// 6: 4439178393
// 7: 4911573711
// 8: 720792880080560128
// 9: 720805087124267008
// 10: 172926330
// Page 3, Number of Ids: 5000
// 0: 93867176
// 1: 2992946183
// 2: 2825296077
// 3: 3784572861
// 4: 2150051321
// 5: 2460881603
// 6: 4128849341
// 7: 2234697931
// 8: 2379418164
// 9: 3425171542
// 10: 325759186
// Page 4, Number of Ids: 5000
// 0: 22793412
// 1: 3347750489
// 2: 316923043
// 3: 2481719196
// 4: 3363591905
// 5: 3238116492
// 6: 58467130
// 7: 3015182362
// 8: 2985342719
// 9: 3095965720
// 10: 17505957
// Page 5, Number of Ids: 5000
// 0: 2541434684
// 1: 140022957
// 2: 134845054
// 3: 772810508
// 4: 16979294
// 5: 2320540225
// 6: 105439442
// 7: 2796744529
// 8: 251128801
// 9: 350229758
// 10: 2683994716
//
destroy loo_Json
destroy loo_Rest
destroy loo_Oauth1
destroy loo_JsonResponse
destroy loo_SbNextCursor