Twitter API cursor not showing in .json (Python)

678 views Asked by At

I read about the cursor documentation in Twitter's API here

I want to be able to see next_cursor_str from.json

I used the code from this twitter's gidhub here

But when I used url = "https://api.twitter.com/2/tweets/search/recent?max_results=100&query={}&{}&cursor=-1".format(query, tweet_fields)

The error:

Exception: (400, '{"errors":[{"parameters":{"cursor":["-1"]},"message":"The query parameter `[cursor] is not one of [query,start_time,end_time,since_id,until_id,max_results,next_token,expansions,tweet.fields,media.fields,poll.fields,place.fields,user.fields]"}],"title":"Invalid Request","detail":"One or more parameters to your request was invalid.","type":"https://api.twitter.com/2/problems/invalid-request"}')`

How can I fixed this so I can use cursoring?

1

There are 1 answers

1
Andy Piper On BEST ANSWER

Cursoring as documented on that page applies to version 1.1 of the Twitter API, and you are targeting v2. You've also linked to a streaming API sample code example, but you've specified a REST recent search endpoint, which does not support streaming.

You need to use the next_token from the response to paginate in v2.

So you get the first page of results using:

https://api.twitter.com/2/tweets/search/recent?query=snow

And then take the next_token from the response and use it in the next query, for example:

https://api.twitter.com/2/tweets/search/recent?query=snow&next_token=b26v89c19zqg8o3fobd8v73egzbdt3qao235oql