pythontweepytwitterapi-python

Problem using tweepy the error 403 Forbidden apeared without making any changes


Hello there thanks for reading my post. I was using this same code yesterday and it was okay but today stoped working and i got this error:

raise Forbidden(response) 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.

This is the code I implemented:

import tweepy
import csv

#Autorización
bearer_token = ""
client = tweepy.Client(bearer_token=bearer_token)

#Get the user id
user=""
user_id= client.get_user(username=user).id

# Get all the tweets of a user 
paginator = tweepy.Paginator(
                                client.get_users_tweets,               # The method you want to use
                                user_id,                            # Some argument for this method
                                #end_time=datetime.datetime(2022, 3, 3, 19, 4, 49),
                                tweet_fields=["created_at", 
                                              "public_metrics",
                                              "possibly_sensitive",
                                              "attachments",
                                              "in_reply_to_user_id"],
                                max_results=100,                       # How many tweets per page
                                limit=40                                # How many pages to retrieve
                            )
data=[]
count=0

#Create a csv file to store the data
filename="@"+user+"_twitterdata.csv"
f = open(filename,'a', newline='')
writer = csv.writer(f)

#Store the data
for tweet in paginator.flatten(limit=40000): # Total number of tweets to retrieve
    count=count+1
    #Check if tha tweet has multimedia content
    if tweet.attachments!=None:media=True
    else:media=False
    #Check if tha tweet is a retweet
    Retweet=tweet.text[:2]=="RT"
    #Check if tha tweet is a repy
    Reply=tweet.in_reply_to_user_id!=None
    
    row=[tweet.id,
          tweet.created_at,
          tweet.public_metrics.get("retweet_count"),
          tweet.public_metrics.get("reply_count"),
          tweet.public_metrics.get("like_count"),
          tweet.public_metrics.get("quote_count"),
          tweet.public_metrics.get("impression_count"),
          tweet.possibly_sensitive,
          media, 
          #tweet.text.replace(",", "" ),
          Retweet,
          Reply
          #tweet.context_annotations
          ]
    writer.writerow(row)
    print("Retrieved ",count," tweets")

# Write the data and close file
writer = csv.writer(f)
f.close()

I tried to generate new keys in the developer portal but that didn't help. Also i tried to change the User autentication settings in the twitter developer portal but that didn't work


Solution

  • With the "free" plan of the Twitter API you can no longer lookup tweets, GET /2/tweets/:id is only available in the "basic" plan, see: https://developer.twitter.com/en/portal/products/free