How to acess tweets with bearer token using tweepy, in python?
Asked Answered
C

6

20

When I signed up for the Twitter API for research , they gave me 3 keys: API Key, API Secret Key, and Bearer Token. However the Hello Tweepy example, 4 keys are used: consumer_key, consumer_secret, access_token, access_token_secret. Obviously, the first two keys map to each other, but I don't see how consumer_secret and access_token map to Bearer Token. I am using this:

CONSUMER_KEY = 'a'
CONSUMER_SECRET = 'b'
ACCESS_TOKEN = 'c'
ACCESS_TOKEN_SECRET = 'd'
BEARER_TOKEN='e'


# Set Connection
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth, wait_on_rate_limit=True)

Where should I use the Bearer token? Thanks

Connoisseur answered 11/2, 2021 at 14:45 Comment(0)
S
3

Unfortunately at this time, you will not be able to use Tweepy for accessing the new full archive search endpoint for academic research. They are working on v2 support, but right now, you'd end up hitting the v1.1 standard search API.

If you are using Python I would suggest taking a look at the Twitter API v2 sample code, or the search_tweets client that Twitter provides. You can then use the BEARER TOKEN by adding it as an environment variable, or if you prefer by adding it directly into the code, but if you do that, be careful not to accidentally commit it to source control where others might get access to it.

To answer the piece about the consumer key/secret vs access token/secret vs bearer token:

  • the bearer token is granted based on the consumer key and secret, and represents just the application identity and credential
  • the access token and secret represent the user identity. If you are using those, you don't use the bearer token, you use that pair in combination with consumer key and secret instead.

In Tweepy terms, the Bearer token would be retrieved by the AppAuthHandler automatically, and the OAuthHandler would not be used in that case.

Scathing answered 11/2, 2021 at 15:25 Comment(2)
Any chance this has changed over the last month? Just got access to Academic and need full-archive. Only used Tweepy previously and would hate have to rewrite my processing formats if I could avoid it.Silvana
Looks like they just added support: dev.to/twitterdev/….Asymmetric
A
16

I believe the confusion lies in the different terminologies for the variables and the use of these variables.

Terminologies

First explained below, terminology clarification, with different terms referring to the same thing:

Client credentials:

1. App Key === API Key === Consumer API Key === Consumer Key === Customer Key === oauth_consumer_key
2. App Key Secret === API Secret Key === Consumer Secret === Consumer Key === Customer Key === oauth_consumer_secret
3. Callback URL === oauth_callback
 

Temporary credentials:

1. Request Token === oauth_token
2. Request Token Secret === oauth_token_secret
3. oauth_verifier
 

Token credentials:

1. Access token === Token === resulting oauth_token
2. Access token secret === Token Secret === resulting oauth_token_secret

Next, the use of these. Note that bearer Token authenticates requests on behalf of your developer App. As this method is specific to the App, it does not involve any users. Thus you can either go with requests on a user level or at an app level as follows:

Usage

User level (OAuth 1.0a):

api_key = "hgrthgy2374RTYFTY"  # CONSUMER_KEY
api_secret_key = "hGDR2Gyr6534tjkht"  # CONSUMER_SECRET
access_token = "HYTHTYH65TYhtfhfgkt34"  # ACCESS_TOKEN
access_token_secret = "ged5654tHFG"  # ACCESS_TOKEN_SECRET

auth = tweepy.OAuthHandler(api_key, api_secret_key)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)  

App level (OAuth 2.0):

bearer_token = "ABDsdfj56nhiugd5tkggred"  # BEARER_TOKEN
auth = tweepy.Client(bearer_token)
api = tweepy.API(auth)

Or alternatively:

auth = tweepy.AppAuthHandler(consumer_key, consumer_secret)
api = tweepy.API(auth)

[1] https://developer.twitter.com/en/docs/authentication/oauth-1-0a/obtaining-user-access-tokens

[2] https://docs.tweepy.org/en/latest/authentication.html#twitter-api-v2

Ahmed answered 12/3, 2021 at 9:14 Comment(0)
S
3

Unfortunately at this time, you will not be able to use Tweepy for accessing the new full archive search endpoint for academic research. They are working on v2 support, but right now, you'd end up hitting the v1.1 standard search API.

If you are using Python I would suggest taking a look at the Twitter API v2 sample code, or the search_tweets client that Twitter provides. You can then use the BEARER TOKEN by adding it as an environment variable, or if you prefer by adding it directly into the code, but if you do that, be careful not to accidentally commit it to source control where others might get access to it.

To answer the piece about the consumer key/secret vs access token/secret vs bearer token:

  • the bearer token is granted based on the consumer key and secret, and represents just the application identity and credential
  • the access token and secret represent the user identity. If you are using those, you don't use the bearer token, you use that pair in combination with consumer key and secret instead.

In Tweepy terms, the Bearer token would be retrieved by the AppAuthHandler automatically, and the OAuthHandler would not be used in that case.

Scathing answered 11/2, 2021 at 15:25 Comment(2)
Any chance this has changed over the last month? Just got access to Academic and need full-archive. Only used Tweepy previously and would hate have to rewrite my processing formats if I could avoid it.Silvana
Looks like they just added support: dev.to/twitterdev/….Asymmetric
C
1

You don't need to use bearer key. You can find the access keys & secrets that you can use under the bearer key in the section where you get your passwords by logging into your Twitter Developer account.

Cradlesong answered 6/3, 2021 at 13:16 Comment(1)
it better if you share screenshots in answer always if possibleTetrad
P
1

@ScriptCode

but it is still unclear where to use the Bearer Token in tweepy's OAuthHandler and access_token?

The Documentation for tweepy 3.10.0 at https://buildmedia.readthedocs.org/media/pdf/tweepy/latest/tweepy.pdf states under 3.3 OAuth 2 Authentication (using Bearer Token)

auth = tweepy.AppAuthHandler(consumer_key, consumer_secret)
api = tweepy.API(auth)

so you use AppAuthHandler and basically leave out the step:

auth.set_access_token(key, secret)

Of course you have to make sure you have registered a Bearer Token for a read-only App in the Twitter Dev Backend.

I tried it and it worked ...

Psychedelic answered 17/3, 2021 at 15:34 Comment(1)
I have tried it and it works! you need to register an App with read-only permissions in the twitter developer dashboard at developer.twitter.com/en/portal/dashboard and create a Bearer token. Then use the AppAuthHandler as posted above and it works!Psychedelic
A
1

Tweepy has been updated to 4.4.0 which supports Twitter API v2. Here is a sample code given you have Academic Research Account [more examples]:

import tweepy

client = tweepy.Client(bearer_token="add_your_Bearer_Token")

# Replace with your own search query
#replace place_country with the code of your country of interest or remove.
query = 'COVID19 place_country:GB'

# Starting time period YYYY-MM-DDTHH:MM:SSZ (max period back is March 2006)
start_time = '2018-01-01T00:00:00Z'

# Ending time period YYYY-MM-DDTHH:MM:SSZ
end_time = '2018-08-03T00:00:00Z'

#I'm getting the geo location of the tweet as well as the location of the user and setting the number of tweets returned to 10 (minimum) - Max is 100

tweets = client.search_all_tweets(query=query, tweet_fields=['context_annotations', 'created_at', 'geo'], place_fields=['place_type', 'geo'], user_fields=['location'], expansions='author_id,geo.place_id', start_time=start_time, end_time=end_time, max_results=10)

# Get list of places and users
places = {p["id"]: p for p in tweets.includes['places']}
users = {u["id"]: u for u in tweets.includes['users']}

#loop through the tweets to get the tweet ID, Date, Text, Author ID, User Location and Tweet Location
for tweet in tweets.data:
    print(tweet.id)
    print(tweet.created_at)
    print(tweet.text)
    print(tweet.author_id)
    if users[tweet.author_id]:
        user = users[tweet.author_id]
        print(user.location) #note that users can add whatever they want as location
    if places[tweet.geo['place_id']]:
        place = places[tweet.geo['place_id']]
        print(place.full_name)
        print("================")
Alasdair answered 22/11, 2021 at 21:27 Comment(0)
S
0

From Tweepy documentation (OAuth 2 Authentication)

Tweepy also supports OAuth 2 authentication. OAuth 2 is a method of authentication where an application makes API requests without the user context. Use this method if you just need read-only access to public information.

So basically, since your app just requires read-only access, you don't need "Access Token" & "Access token secret" and can ignore the 3rd & 4th Steps. A simple code for this solution would be as follow:

auth = tweepy.AppAuthHandler(consumer_key, consumer_secret)
api = tweepy.API(auth)
for tweet in tweepy.Cursor(api.search, q='cool').items(3):
    print(tweet.text)
Subotica answered 1/5, 2021 at 2:25 Comment(1)
I got this error on your code: AttributeError: 'API' object has no attribute 'search'Cattleman

© 2022 - 2024 — McMap. All rights reserved.