Questioner
Questioner

Reputation: 716

Twitter API access denied for posting a simple tweet

Using the below python code, while the authentication is successful, I get the following error:

Error Code: 453: 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. You can learn more here: https://developer.twitter.com/en/portal/product

I am currently using the free version on developer.twitter.com.

Code:

import tweepy

# Authenticate to Twitter
auth = tweepy.OAuthHandler("CONSUMER_KEY", "CONSUMER_SECRET")
auth.set_access_token("ACCESS_TOKEN", "ACCESS_TOKEN_SECRET")

# Create API object
api = tweepy.API(auth)

try:
    api.verify_credentials()
    print("Authentication OK")
except:
    print("Error during authentication")

# Create a tweet
api.update_status("content of tweet")

In this link, the right access is described as follows:

Free

  • For write-only use cases and testing the Twitter API
  • Rate limited access to v2 tweet posting and media upload endpoints
  • 1,500 Tweets per month - posting limit at the app level
  • 1 app ID
  • Login with Twitter

And this is error log:

Authentication OK

Traceback (most recent call last): File "...\create_tweet.py", line 19, in api.update_status('content of tweet') File "...\tweepy\api.py", line 46, in wrapper
return method(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^ File "C:...\tweepy\api.py", line 979, in update_status return self.request( ^^^^^^^^^^^^^ File "C:...\tweepy\api.py", line 271, in request
raise Forbidden(resp) tweepy.errors.Forbidden: 403 Forbidden 453 - 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. You can learn more here: https://developer.twitter.com/en/portal/product

Upvotes: 4

Views: 5445

Answers (3)

Yacine Boudiaf
Yacine Boudiaf

Reputation: 47

It is still possible to post tweets.

How to post a simple tweet:

The following code shows how to post a simple tweet without an image using python tweepy.

api_v2 = tweepy.Client(
    consumer_key=self.consumer_key,
    consumer_secret=self.consumer_secret,
    access_token=self.access_token,
    access_token_secret=self.access_token_secret)

response = api_v2.create_tweet(text='random tweet text')

How to post a tweet with an image (media upload):

The media upload is not yet supported in the version 2. As a result, to upload the media, the version 1.1 can still be used.

The following code shows an example of how to post a tweet with an image using python tweepy:

# use api version 1.1 to upload the media:
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api_v1 = tweepy.API(auth)

# upload the media:
# note that the api version 1.1 is used to upload the media
# and get the media_id.
media = api_v1.media_upload(
    filename=filename,
    media_category=category,
    chunked=(category != 'tweet_image'),
    file=data)
media_id = media.media_id

# create the tweet:
if media_id:
    api_v2.create_tweet(text='tweet with an image',
                        media_ids=[media_id])
else:
    api_v2.create_tweet(text='tweet without an image')

Upvotes: 0

Viktor Brešan
Viktor Brešan

Reputation: 5521

As mentioned in the comments under your question, it is possible to post tweets using V2 endpoints. These endpoints are supported by tweepy, and a working code sample would be as follows:

import tweepy
    
client = tweepy.Client(
    consumer_key=API_KEY,
    consumer_secret=API_KEY_SECRET,
    access_token=ACCESS_TOKEN,
    access_token_secret=ACCESS_TOKEN_SECRET
)

client.create_tweet(text='Test.')

Edit 1: note that creating a tweet with duplicate content will result with tweepy.errors.Forbidden: 403 Forbidden.

Edit 2: Place yourself in tab Free on https://developer.twitter.com/en/portal/products, and make sure that your project is listed there. Your app has to be under the project, i.e. it can't be standalone app anymore.

Upvotes: 6

user2267379
user2267379

Reputation: 1137

this code works for me (all 5 keys are require for authentification) :

import tweepy

client = tweepy.Client(bearer_token=BEARER, consumer_key=CONSUMER_KEY, consumer_secret=CONSUMER_SECRET, access_token=ACCESS_KEY, access_token_secret=ACCESS_SECRET)

client.create_tweet(text="Yeah boy! I did it")

Source : https://stackoverflow.com/a/70062360/2267379

Upvotes: 2

Related Questions