2

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!

Ebikeneser
  • 2,582
  • 13
  • 57
  • 111
  • Your mistake is thinking that Twitter is going to provide functional anything these days. Your own experience shows that Twitter is broken as hell in multiple ways. – Slbox Aug 03 '23 at 00:25

1 Answers1

1

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.

enter image description here

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.

enter image description here

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.

enter image description here

Life is complex
  • 15,374
  • 5
  • 29
  • 58
  • This returns the same error response form the api call. I think there is a fundamental issue with the endpoint here in the background of twitter. Have you ran your code with a basic level developer account and successfully retrieved the followers with the code as of today? – Ebikeneser Aug 02 '23 at 09:46
  • The code that I posted no longer works, because Twitter removed specific API calls in June 2023. I'm not sure if there is a workaround based on the GitHub chatter. – Life is complex Aug 02 '23 at 11:32
  • Very annoying, the dev portal states the endpoints are still accessible under the Basic plan (hence why I payed $100 for it) and the change log states differently. Twitter or should I say X is all over the place just now. – Ebikeneser Aug 03 '23 at 08:15
  • I agree with you. There is conflicting information all over the dev portal about Access Levels. Some pages talk about `X` and others talk about `Twitter`. I wonder what `X` will be in the future... – Life is complex Aug 03 '23 at 14:14