Hi,

I am trying to get tweets metrics data with query as follows:
https://api.twitter.com/2/tweets/search/recent?query=from%3Atwitterdev&tweet.fields=public_metrics&expansions=author_id&user.fields=description

I tried this with bearer_token in postman and it worked but when I try to replicate it in the python script, I got error-401.

Is there an example of python script with ouath2 verification with bearer_token?

better give an example of your code in handling authentication bearer code

Here are 2 approaches I tried:
1.url = ‘https://api.twitter.com/2/tweets/search/recent?query=from%3Atwitterdev&tweet.fields=public_metrics&expansions=author_id&user.fields=description’
headers = {‘Bearer’: BEARER_TOKEN}
import requests
r = requests.get(url = url, headers=headers)
print(r.dict)
This gives error-‘_content’: b’{\n “title”: “Unauthorized”,\n “type”: “about:blank”,\n “status”: 401,\n “detail”: “Unauthorized”\n}’

2.consumer = oauth.Consumer(key=CONSUMER_KEY, secret=CONSUMER_SECRET)
access_token = oauth.Token(key=ACCESS_TOKEN, secret=ACCESS_TOKEN_SECRET)
client = oauth.Client(consumer, access_token)
endpoint = “https://api.twitter.com/2/tweets/search/recent?query=from%3Atwitterdev&tweet.fields=public_metrics”
response, data = client.request(endpoint)
tweets = json.loads(data)
print(tweets)
This gives error-
{‘client_id’: ‘app_id’, ‘detail’: ‘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.’, ‘registration_url’: ‘Projects overview | Docs | Twitter Developer Platform’, ‘title’: ‘Client Forbidden’, ‘required_enrollment’: ‘Standard Basic’, ‘reason’: ‘client-not-enrolled’, ‘type’: ‘Twitter API Response Codes & Error Support | Twitter Developer Platform’}.

is your application in a project or stand alone?

its in a project. I am using app keys an tokens from the dashboard.

If you look at the sample code for v2 you will need to reformat this header, something like:

    r.headers["Authorization"] = f"Bearer {bearer_token}"
3 Likes

Thanks it worked and I am able to fetch the public metric for tweets.
I also want to get non_public_metrics and other restricted fields. I am currently using bearer token method of auth. But it doesnt seem enough on permissions.
Which auth method shall I use for app to make api calls get this private data? I have tried 3-legged flow but it need manual intervention to click on link and get outh_vefirier.
What is the correct approach for an app to achieve this?
Thanks in Advance!

Bearer auth won’t work - you need a user’s access token & secret, and sign the request with oAuth1.0a. You need the consumer key & secret (also called the API key and secret) and the Access Token & Secret - which you either get from the dashboard (for your own account only) or from the Login with twitter flow at the end - you need the access token and access token secret though, not the oauth verifier. I recommend using a library for this because doing it manually is very error prone.

Try TwitterAPI/expansions_tweet.py at master · geduldig/TwitterAPI · GitHub this example, but add non_public_metrics to tweet fields and attachments.media_keys&media.fields=non_public_metrics,organic_metrics to expansions like in Metrics | Docs | Twitter Developer Platform

You will only be able to see private metrics for tweets that the user has authored.

2 Likes

Thanks, that worked.
I have few other owned twitter accounts for which I need to get same data eg tweet public and non_public metric.

  1. Can I use the same app with same keys? If yes, how?
  2. Do I have to create project and app for each of those accounts?

Thanks in advance.

Yes, and you only need 1 App, all the other accounts don’t even need anything else. You’ll just need to log in as the other accounts in your browser, follow these instructions to do it manually without implementing anything:

1 Like

do I need to configure my 0auth1.0?