Hi,
I’m using Python’s Tweepy package( version 4.5 - supports v2 Twitter API) to access the Twitter v2 API calls. I get Unauthorized 401 error, when I use tweepy.Client() functions.

Below is my code,

import tweepy
client = tweepy.Client(consumer_key= consumer_key,consumer_secret= consumer_secret,access_token= access_token,access_token_secret= access_token_secret)
query = ‘news’
tweets = client.search_recent_tweets(query=query, max_results=10)

Below is the error trace,
File “/opt/anaconda3/envs/tweepyv2/lib/python3.8/site-packages/tweepy/client.py”, line 848, in search_recent_tweets
return self._make_request(
File “/opt/anaconda3/envs/tweepyv2/lib/python3.8/site-packages/tweepy/client.py”, line 168, in _make_request
response = self.request(method, route, params=request_params,
File “/opt/anaconda3/envs/tweepyv2/lib/python3.8/site-packages/tweepy/client.py”, line 122, in request
raise Unauthorized(response)
tweepy.errors.Unauthorized: 401 Unauthorized

When using the tweepy.api (standard v1.1), the consumer_key, consumer_scret, access_token, acess_token_secret are applied successfully and I get the tweets.

But the same tokens do not work when I use tweepy.Client() ( Twitter v2 API).

Could you please help me resolve this issue?

Did you add the exact same app to the v2 Project on https://developer.twitter.com/en/portal/projects-and-apps ?

@IgorBrigadir Yes. The project says it has elevated access. And the exact same app is under the project


1 Like

Instead of using access tokens and secret, does the bearer token work?

import tweepy

client = tweepy.Client("Bearer Token here")

And if that one doesn’t either, does resetting the keys work?

The bearer token did not work. I will try resetting the keys. Which key needs to be reset? Do you mean all - api keys, bearer token or access token?

It should be enough to reset the access tokens and bearer token to try again - i’m just guessing though, no idea why it wouldn’t work if the tokens are correct for v1.1 and the project is configured correctly.

Does it work if you make “twitter_topic_test” a “Production App” in the project instead?

1 Like

@IgorBrigadir .
Thanks for your help. I created a new Prod app in the environment and generated a new bearer token and it worked.
I’m getting tweets for query ‘news’. But I’m not able to access the tweet’s public_metrics such as likes, impressions which I need to implement for my project…
The doucment here(link: Metrics | Docs | Twitter Developer Platform) says, Public metrics can be requested with OAuth 2.0 Bearer Token authentication.

What does that mean? I have not yet generated Access Token and Secret yet. Should this be generated? In the Oath 2.0 form, it says add callBack URL, what is that?

1 Like

I created a new Prod app in the environment and generated a new bearer token and it worked.

Great! This might help others who get the same. I think it’s a bug on twitter’s end if the dev environment doesn’t work but the prod one for the same project does.

public_metrics should be available if you request the right expansions and fields Using fields and expansions | Docs | Twitter Developer Platform only private metrics require Access token and secret that belongs to a user. In tweepy, private_metrics should be in the tweet object as a property.

If you’re using a bearer token you’re already using oauth2.0. Callback URL is for implementing steps in the authentication flow, which isn’t relevant if you’re not authenticating other users, and using only your own account.

Hope that helps!

1 Like

Understood. I will check the link out for public metrics.

Thanks a lot for your help, @IgorBrigadir.

1 Like

Hi @IgorBrigadir ,
I had another question.
I was successfully able to get the public metrics.
With the below code:
tweets = client.search_recent_tweets(query=query, max_results=10, tweet_fields=[“public_metrics”])

But I was not able to get non_public_metrics and organic metrics with the same method.
The documentation mentions, Non-public metrics can be requested for owned/authorized Tweets only. This means developers are required to authenticate using OAuth 1.0a User Context authorization.

Could you share how to do the OAuth 1.0a User Context?

Sure, in Tweepy it’s Authentication — tweepy 4.5.0 documentation

1 Like

Thanks, @IgorBrigadir for sharing the link.
I was able to OAuth 1.0a authorize myself.

I’m trying to access the non_public_metrics, with below code:
query=‘news’
tweets = client.search_recent_tweets(query=query, max_results=10, tweet_fields=[“non_public_metrics”])

I received ‘You are not authorized to access non_public_metrics error’.

Can we access these non_public_metrics for any tweet with our consumer key ? If yes, then how?

Or non_public_metrics are only for the tweets I tweet with my user token?

1 Like

Unfortunately this isn’t going to work, you can only view private metrics for tweets you have authored - specifically the tweets that belong to the access token you used Metrics | Docs | Twitter Developer Platform

Thanks, @IgorBrigadir for your response. I really appreciate you helping me.
I have one final question.

In the standard v1.1, we have an operator to search tweets with the retweets filtered. This results in tweets without any retweets/
The query string will be like,
query = “news -filter:retweets”

But in the Twitter v2 documentation, Search Tweets integration reference | Docs | Twitter Developer Platform I don’t see an option to search tweets with retweets filtered.

Is it possible to search tweets with retweets filtered?

Sure, it’s -is:retweet and the list of operators is here: Search Tweets - How to build a query | Docs | Twitter Developer Platform (you can use any ones listed as “Core” availability if you don’t have academic access.)

Awesome. Thanks a lot.

1 Like