I am trying to retrieve a list of who a Twitter account follows using Python.
After realizing that the free tier API access did not provide this endpoint I upgraded my developer account to the basic plan (for $100 a month) as it clearly states that once signed up, you can retrieve an accounts followers or following.
I have created a script that should retrieve the followers of an account based on a user id -
import requests
def get_followers(user_id, bearer_token):
url = f'https://api.twitter.com/2/users/{user_id}/followers'
headers = {'Authorization': f'Bearer {bearer_token}'}
all_followers = []
while True:
response = requests.get(url, headers=headers)
if response.status_code == 200:
result_data = response.json()
all_followers.extend(result_data['data'])
if 'next_token' in result_data['meta']:
next_token = result_data['meta']['next_token']
url = f'https://api.twitter.com/2/users/{user_id}/followers?pagination_token={next_token}'
else:
break
else:
print(f"Failed to get followers for user ID '{user_id}' with status code: {response.status_code}")
print(response.json())
return None
return all_followers
However I am getting the following (quite common it would seem) error response -
{
"client_id":"my-id",
"detail":"When authenticating requests to the Twitter API v2 endpoints, you must use keys and tokens from a Twitter developer App that is attached to a Project. You can create a project via the developer portal.",
"registration_url":"https://developer.twitter.com/en/docs/projects/overview",
"title":"Client Forbidden",
"required_enrollment":"Appropriate Level of API Access",
"reason":"client-not-enrolled",
"type":"https://api.twitter.com/2/problems/client-forbidden"
}
I have made sure that my application is located within a project that has the V2 ACCESS
tag associated to it.
I also tried using Tweepy however was met with the same error response.
Also on reading the specific page in the Twitter docs, the quick start guide AND the API explorer buttons both leads to broken links!