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
6
Answers
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:
In Tweepy terms, the Bearer token would be retrieved by the
AppAuthHandler
automatically, and theOAuthHandler
would not be used in that case.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.
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:
Temporary credentials:
Token credentials:
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):
App level (OAuth 2.0):
Or alternatively:
[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
@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)
so you use AppAuthHandler and basically leave out the step:
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 …
From Tweepy documentation (OAuth 2 Authentication)
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:
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]: