Search code examples
pythonapitwittertweepybearer-token

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


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


Solution

  • 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.