问题
I am creating a small program to get a instagram users following list using C# and instasharp. The code below will get me the first 50. I believe I have to use the pagination option to get the next url to get to the next page. Thats where Im stuck. How do I use pagination to loop through all of a users following?
var config = new InstaSharp.InstagramConfig("api", "oauth", "xxxxxx", "xxxxxxx", "xxxxxx");
var config1 = new InstaSharp.Endpoints.Relationships.Unauthenticated(config);
var result = config1.Follows(000000);
dynamic dyn = JsonConvert.DeserializeObject(result.Json);
foreach (var data in dyn.data)
{
listBox1.Items.Add(data.username);
}
回答1:
Based on my response here: https://stackoverflow.com/a/25139236/88217
If you look at the unit tests on the InstaSharp github, you can see an example of how to use Pagination:
public async Task Follows_NextCursor()
{
//This test will fail if testing with an account with less than one page of follows
var result = await relationships.Follows();
result = await relationships.Follows(457273003/*ffujiy*/, result.Pagination.NextCursor);
Assert.IsTrue(result.Data.Count > 0);
}
In the case where you want to loop through and get all of them, I would expect you to do something like this:
int userID = 000000;
var result = await relationships.Follows(userID);
while(result.Data.Count > 0)
{
dynamic dyn = JsonConvert.DeserializeObject(result.Json);
foreach (var data in dyn.data)
{
listBox1.Items.Add(data.username);
}
result = await relationships.Follows(userID, result.Pagination.NextCursor)
}
Because this code uses await, you will have to mark the method as async, for more information on what that entails, I would suggest looking at the async/await keywords, a good introduction can be found at Stephen Cleary's Blog
来源:https://stackoverflow.com/questions/25137768/c-sharp-instasharp-how-to-use-pagination