Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C) Twitter - Get Follower IDsDemonstrates 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. Note: Rate limits will prevent a program from paging through huge lists of followers. In general, a maximum of 16 GET requests (pages) can be retrieved in a short period of time.
#include <C_CkJsonObjectW.h> #include <C_CkRestW.h> #include <C_CkOAuth1W.h> #include <C_CkStringBuilderW.h> void ChilkatSample(void) { HCkJsonObjectW json; BOOL success; HCkRestW rest; HCkOAuth1W oauth1; BOOL bAutoReconnect; HCkJsonObjectW jsonResponse; HCkStringBuilderW sbNextCursor; int pageNum; BOOL caseSensitive; BOOL bContinue; const wchar_t *resp; int numIds; int i; // 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.. json = CkJsonObjectW_Create(); success = CkJsonObjectW_LoadFile(json,L"qa_data/tokens/twitter.json"); rest = CkRestW_Create(); oauth1 = CkOAuth1W_Create(); CkOAuth1W_putConsumerKey(oauth1,L"TWITTER_CONSUMER_KEY"); CkOAuth1W_putConsumerSecret(oauth1,L"TWITTER_CONSUMER_SECRET"); CkOAuth1W_putToken(oauth1,CkJsonObjectW_stringOf(json,L"oauth_token")); CkOAuth1W_putTokenSecret(oauth1,CkJsonObjectW_stringOf(json,L"oauth_token_secret")); CkOAuth1W_putSignatureMethod(oauth1,L"HMAC-SHA1"); CkOAuth1W_GenNonce(oauth1,16); CkRestW_SetAuthOAuth1(rest,oauth1,FALSE); bAutoReconnect = TRUE; success = CkRestW_Connect(rest,L"api.twitter.com",443,TRUE,bAutoReconnect); if (success != TRUE) { wprintf(L"%s\n",CkRestW_lastErrorText(rest)); CkJsonObjectW_Dispose(json); CkRestW_Dispose(rest); CkOAuth1W_Dispose(oauth1); return; } // This ends the initial setup... // ---------------------------------------------------------------------- // This Twitter user has about 77.5K followers.. CkRestW_AddQueryParam(rest,L"screen_name",L"MarcusMiller959"); jsonResponse = CkJsonObjectW_Create(); CkJsonObjectW_putEmitCompact(jsonResponse,FALSE); // Get the 1st page of results using a cursor of "-1". sbNextCursor = CkStringBuilderW_Create(); CkStringBuilderW_SetString(sbNextCursor,L"-1"); pageNum = 1; caseSensitive = FALSE; bContinue = TRUE; // Get a maximum of 5 pages (of 5000 ids each). while (bContinue == TRUE) { if (CkStringBuilderW_ContentsEqual(sbNextCursor,L"0",caseSensitive) == TRUE) { // This will cause the loop to exit.. bContinue = FALSE; } else { // Adds or replaces the query param. CkRestW_AddQueryParam(rest,L"cursor",CkStringBuilderW_getAsString(sbNextCursor)); // Get the next page of follower IDs resp = CkRestW_fullRequestNoBody(rest,L"GET",L"/1.1/followers/ids.json"); if (CkRestW_getLastMethodSuccess(rest) != TRUE) { wprintf(L"%s\n",CkRestW_lastErrorText(rest)); CkJsonObjectW_Dispose(json); CkRestW_Dispose(rest); CkOAuth1W_Dispose(oauth1); CkJsonObjectW_Dispose(jsonResponse); CkStringBuilderW_Dispose(sbNextCursor); return; } CkJsonObjectW_Load(jsonResponse,resp); if (CkRestW_getResponseStatusCode(rest) != 200) { wprintf(L"%s\n",CkJsonObjectW_emit(jsonResponse)); CkJsonObjectW_Dispose(json); CkRestW_Dispose(rest); CkOAuth1W_Dispose(oauth1); CkJsonObjectW_Dispose(jsonResponse); CkStringBuilderW_Dispose(sbNextCursor); return; } // Show the number of IDs returned in this numIds = CkJsonObjectW_SizeOfArray(jsonResponse,L"ids"); wprintf(L"Page %d, Number of Ids: %d\n",pageNum,numIds); // Show the 1st 10 ids in this page. i = 0; while (i < numIds) { CkJsonObjectW_putI(jsonResponse,i); wprintf(L" %d: %s\n",i,CkJsonObjectW_stringOf(jsonResponse,L"ids[i]")); i = i + 1; if (i > 10) { // Force the loop to exit. i = numIds; } } pageNum = pageNum + 1; if (pageNum > 5) { bContinue = FALSE; } else { CkStringBuilderW_SetString(sbNextCursor,CkJsonObjectW_stringOf(jsonResponse,L"next_cursor_str")); } } } // 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 // CkJsonObjectW_Dispose(json); CkRestW_Dispose(rest); CkOAuth1W_Dispose(oauth1); CkJsonObjectW_Dispose(jsonResponse); CkStringBuilderW_Dispose(sbNextCursor); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.