As described in the Twitter Error Codes and Responses, a 429
response code means Too Many Requests
. Therefore, the error is not in the code syntax per se, but instead in the number of calls you are performing on the Twitter API. Please take a look at the REST API Rate Limiting documentation to learn how to track how many calls you can perform (especially using the X-Rate-Limit-Remaining
and other HTTP headers), and the rate limiting details for each REST endpoint.
Regarding your question about how to get to paginate after the first 20 results, check using cursors. Here, the condition on the cursor should be while cursor != 0:
in order to be able to go to the next page. Then, it involves making sure you are not making too many calls to the Twitter API.
That being said, here is a better solution for you leveraging GET friends/ids
. This enables you to retrieve the ids of users you follow, 5000 at once (instead of 20), and you can hydrate them just after using GET users/lookup
. This will work for a large number of followings without the need to pause between each individual calls:
def get_follow_list():
users = []
users_ids = []
# Fetch the followings as a cursored collection (up to 5000 per call).
cursor = -1
while cursor != 0:
response = twitter.get_friends_ids(screen_name=current_user, cursor=cursor)
users_ids += response['ids']
cursor = response['next_cursor']
# Lookup the users by chunks of 100.
for i in range(0, len(users_ids), 100):
chunk = users_ids[i:i + 100]
users += twitter.lookup_user(user_id=chunk)
# Return the user objects.
return (users)