Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 458
Carolina Ferraz Marreiros Avatar asked Feb 11 '21 14:02

Carolina Ferraz Marreiros


People also ask

How do you get a bearer token in Tweepy?

The simplest way to generate a bearer token is through your app's Keys and Tokens tab under the Twitter Developer Portal Projects & Apps page.


1 Answers

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:

User level (OAuth 1.0a):

API key:"hgrthgy2374RTYFTY"
API key secret:"hGDR2Gyr6534tjkht"
Access token:"HYTHTYH65TYhtfhfgkt34"
Access token secret: "ged5654tHFG"

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(key, secret)
api = tweepy.API(auth)  

App level (OAuth 2.0):

Bearer token: "ABDsdfj56nhiugd5tkggred"
auth = tweepy.Client("Bearer Token here")
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

like image 81
ScriptCode Avatar answered Sep 28 '22 04:09

ScriptCode