C4dev
#1
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
C4dev
#3
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?
C4dev
#5
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
C4dev
#7
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
C4dev
#9
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.
- Can I use the same app with same keys? If yes, how?
- 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?