I’m trying to authenticate to twitter’s new API (v2) using tweepy and retrieve tweets but encounter a strange error related to the authentication process.
I’m currently using the free access to the API.
Code sample :
import tweepy
# Authentification OAuth 1.0a User Context to retrieve my own data
dict_twitter_api = {
"consumer_key": "blah",
"consumer_secret": "blah",
"access_token": "blah",
"access_token_secret": "blah"
}
client = tweepy.Client(**dict_twitter_api)
# If you're working behind a corporate proxy,
# client.session.proxies = {
# "http": "my-corporate-proxy",
# "https": "my-corporate-proxy",
# }
print(client.get_me()) # <-- this works well
print(client.get_home_timeline())
Traceback result :
> 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’ve checked my different tokens and confirmed that the OAuth 1.0a user context authentication should be working to retrieve my own timeline.
2
Answers
The returned exception is in fact not related to the authentication process.
Twitter's free API access does not allow you to retrieve your own timeline or event tweets at all. This is stated in the about the twitter API documentation's page; it also explains why the page on rate limits only details the 'Basic' access'rates and bears no mention of the 'Free' access...
Nota: in definitive, this appears to be a "no-code-related" question/answer; it may still prove usefull as 1) this is not referenced enough as yet and 2) the current traceback would throw any developer on the wrong path... Given the circonstances, I'll also share this resource which might be more uptodate/thorough.
Musk made this API endpoints paid.
In api v2 free tier you can access only to post tweets and GET /2/users/me.
source: https://developer.twitter.com/en
Here is table what you can get from
/2/users/me
https://developer.twitter.com/en/docs/twitter-api/data-dictionary/object-model/user