Getting the 403 forbidden error when read data from the Twitter Using APIs

303 Views Asked by At

I have a free Twitter account, created a project, created an app. When I extract data from the twitter using APIs I get forbidden: You currently have access to a subset of Twitter API v2 endpoints and limited v1.1 endpoints (e.g. media post, oauth) only. If you need access to this endpoint, you may need a different access level.

I tried to re-created one another Twitter account, and did it again, but the problem still happened. I search web and there are many same problems for reading twitter text in APIs recently. However, no one solved this error until now.

1

There are 1 best solutions below

0
On

/2/tweets/search/recent is not available in the free plan. See V2 endpoints available on the free plan here: https://developer.twitter.com/en/portal/products/free For searching recent tweets, you will need to upgrade to basic plan, which is currently 100$/month

enter image description here

Also the pro plan has the permission for FULL-ARCHIVE SEARCH which the basic plan doesn't support

Here is a simple python code to immediately check if you are getting tweets after upgrading to basic plan.

import requests
import os

# Replace with your Twitter Bearer Token
bearer_token = os.getenv("TWITTER_BEARER_TOKEN")

# API endpoint URL
url = "https://api.twitter.com/2/tweets/search/recent"

# Parameters for the API request
params = {
    "query": "SEARCH_WORD",
    "max_results": 20  # 10-100 in basic plan
}

# Headers with Bearer Token
headers = {
    "Authorization": f"Bearer {bearer_token}"
}

try:
    # Make the API request
    response = requests.get(url, params=params, headers=headers)
    response.raise_for_status()  # Check for any request errors

    # Parse the JSON response
    data = response.json()

    # Check if the response contains tweets
    if "data" in data:
        # Print each tweet with tweet URL
        for tweet in data["data"]:
            tweet_id = tweet["id"]
            tweet_text = tweet["text"]
            tweet_url = f"https://twitter.com/user/status/{tweet_id}"
            print(f"Tweet ID: {tweet_id}\nTweet Text: {tweet_text}\nTweet URL: {tweet_url}\n")

    else:
        print("No tweets found in the response.")

except requests.exceptions.RequestException as e:
    print(f"Error making the API request: {e}")

except json.JSONDecodeError as e:
    print(f"Error parsing the JSON response: {e}")