Twitter API access denied for posting a simple tweet
Asked Answered
U

3

3

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

Unbidden answered 7/6, 2023 at 1:36 Comment(9)
Free tier may not give you access anymore. "Starting February 9, we will no longer support free access to the Twitter API, both v2 and v1.1. A paid basic tier will be available instead". I wouldn't be surprised if their dev site and docs haven't been updated given how Twitter is currently being managed.Potentiate
@Unbidden if you have created your app (on Twitter developer) after Nov 15th, 2021, is not possible anymore to call the API v. 1.1 to post a tweet for free, see here the table and the note below the table. You can use the API v. 1.1 to: 1) upload media (because the v. 2 API doesn't have the media endpoint yet), and 2) post a tweet using V2 endpoint, with API v. 1.1 authentication as explained here in the last introduction paragraph.Nickey
Of course, you can post a tweet using V2 endpoint combined with API v. 1.1 authentication, but in this case you must use the 3-legged OAuth flow (OAuth 1), as explained here, and here you can find a step by step example. Right now the situation is very confusing, and the info on the Twitter website are fragmented in several pages. Hope this can help you.Nickey
@Nickey , But apparently Twitter has removed the free API access. See here : Twitter shut off its free API and it's breaking a lot of appsUnbidden
@Unbidden the v. 1.1 "OAuth 1" and "media" endpoints still working also for free app (with a rate limit because of the free app). I'm using it on my web app. As I explained in the previous comments, you can use the OAuth 1 (from v. 1.1 API), to make tweet post on v. 2 endpoint, and also to upload media (because the v. 2 API doesn't have yet a media endpoint). Try it ;)Nickey
@Nickey , Do you mean I can tweet an image with the code above? That is, in this way: api.update_status('image_path/image_name.jpg') ? I tried this but got the same error message as before.Unbidden
@Unbidden I use the twitter API in .net, so I don't know how the python implementation works. I didn't used a library like "tweepy" for accessing the Twitter API, I have implemented the API calls by myself (I used only the oauth-dotnetcore library, to generate the OAuth 1 token). What you need to do, is what I explained in the message before: first authenticate the user using the 3-legged OAuth flow (OAuth 1), and then you can post using the v. 2 API endpoint. Try to ask in the tweepy community, because in this case you depend from them.Nickey
@Nickey , I am not sure if this way can do exactly what I intend. I wanna reply to a tweet which contains some keywords. That is, if some keyword exists in a public tweet then my App will reply to it as a comment with a specific text that I determined in my App. But here I can e.g. share an article published in Medium site on behalf of my twitter account. However, I am not sure if it is possible to post such a tweet as a response and comment to a tweet that includes the desired keyword? Hope what I said is clear enough.Unbidden
@Unbidden yes it's clear. The error message said: "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." The problem is related to the API v. 1.1 usage, with a free subscription. Now the free subscription for the v. 1.1 API, can only make call to "media" and "OAuth v1" endpoint. If you wanna call all the old v. 1.1, you need at least the "elevated" subscription see here.Nickey
T
6

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.

Tolbooth answered 12/6, 2023 at 5:13 Comment(8)
this does not work and is not even working code. I am surprised this answer got any votes in the first place.Carisa
@D.L For brevity I have previously omitted the import statement. Corrected. What error message are you receiving?Iives
exactly the same error as in the original question: "exception found: 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: developer.twitter.com/en/portal/product"Carisa
@D.L The code works and I am using it in production daily. Check if you are using the latest tweepy version and if you have set up app on twitter developer portal correctly.Iives
@D.L If I remember correctly, I had to re-create my apps on twitter developer portal.Iives
it was actually working up until today. Post the changes made (by twitter) it does not work. Perhaps you are paying a subscription and this is why ?Carisa
@D.L I am not paying a subscription.Iives
@D.L I have updated my answer with the fix for what might be the problem in your case.Iives
L
2

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://mcmap.net/q/1176307/-post-tweets-using-tweepy-and-twitter-api-v2

Leadin answered 25/6, 2023 at 17:17 Comment(3)
this no longer works. looks like it is time to leave the twitter platform.... any recommendations ?Carisa
@D.L : That's works one week ago.Leadin
Great! This worked for me today after trying several configs. Free account.Subcontinent
T
0

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')
Takin answered 16/12, 2023 at 11:32 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.