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!
My original post here provided the code (now removed) that I wrote for the question Tweepy get followers list on April 18, 2023.
After your comment, I did some more research into the error message:
tweepy.errors.Forbidden: 403 Forbidden
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.
I found that the Twitter API capabilities changed soon after I wrote that code back in April 2023.
This change removed querying access to obtain the followers for an individual user or for a list.
The image below is from Twitter's Developer Changelog, which shows this change.
Here is a GitHub issue on the error in question.
It's interesting that the Twitter Account used by the Twitter's API Developers to post changelog updates is now non-existent on Twitter.
I also looked at the Twitter Access levels. Earlier this year I was using the Essential Account to query for followers.
The X Access Levels are, which are more restrictive as shown in the image below.
Based on these new access levels and the changelog post on June 26, 2023, it seems that you need to have an Enterprise account to obtain a list of followers.