How to acess tweets with bearer token using tweepy, in python? - 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

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

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.

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.

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

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("================")

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)

Related

Tweepy API v2 Authentication

How can I test the Tweepy authentication using API v2?
My code below fails to post due to authentication however, is there any other way to test the authentication without invoking posting a Tweet (client.create_tweet) ?
import tweepy
bearer_token = "hidden"
consumer_key = "hidden"
consumer_secret = "hidden"
access_token = "hidden"
access_token_secret = "hidden"
client = tweepy.Client(bearer_token=bearer_token)
client = tweepy.Client(
consumer_key=consumer_key,
consumer_secret=consumer_secret,
access_token=access_token,
access_token_secret=access_token_secret
)
client.create_tweet(text="My first tweet")
You can use any of the methods listed in the documentation here.
I would suggest you to use some read method like get_user:
user = client.get_user('twitter')
print(user.name)
About your authentication itself, the problem is that you have to choose between:
Using the bearer token of your application to get an OAuth 2 "App-Only" authentication. You will have, basically, the same possibilies as an unregistered user on the website.
Using an account's tokens to get an OAuth 1.0a "User Context" authentication. You will then be able to act in the behalf of the user who possesses the tokens that you are using.
But you are trying to do both successively.
I put the bearer token with the other parameters in tweepy.Client, it's not separated and works every time:
import tweepy
client = tweepy.Client(bearer_token="YOUR_BEARER_KEY",
consumer_key="YOUR_CONSUMER_KEY",
consumer_secret="YOUR_CONSUMER_SECRET",
access_token="YOUR_ACCESS_TOKEN",
access_token_secret="YOUR_ACCESS_TOKEN_SECRET")
You can also add wait_on_rate_limit=True to limit your rates in case your not handling them properly.
For a simple test similar to Mickaël's Answer which prints a twitter username to its respective id:
user_get = client.get_user(username='a_twitter_username_here')
user_id = user_get.data.id
print(user_id)
If this does not work then try regenerating your keys and tokens if you can.
This method does not need elevated access.

Stream encountered HTTP error: 403 Twitter API, but I have elevated access

I have recently received elevated access to Twitter Developers. I have created a new project, and I have OAuth 1.0a turned on with permission to read and write, but when I ran code, I received: Stream encountered HTTP error: 403
import tweepy
from config import ACCESS_TOKEN as access_token
from config import ACCESS_TOKEN_SECRET as access_token_secret
from config import API_KEY as api_key
from config import API_KEY_SECRET as api_key_secret
auth = tweepy.OAuthHandler(api_key, api_key_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
class Linstener(tweepy.Stream):
tweets = []
limit = 1
def on_status(self, status):
self.tweets.append(status)
# print(status.user.screen_name + ": " + status.text)
if len(self.tweets) == self.limit:
self.disconnect()
stream_tweet = Linstener(api_key, api_key_secret, access_token, access_token_secret)
users = ['pawka322']
user_ids = []
for user in users:
user_ids.append(api.get_user(screen_name=user).id)
stream_tweet.filter(follow=user_ids)
What I have done:
Created a new app and saved consumer key and consumer secret
Created a new Development project
Turned on OAuth 1.0a:
Set app permission to Read and Write
Filled User “Callback URI / Redirect URL” and “Website URL” with example org
Generated access token and secret access token
My credentials work fine if I am getting Tweets from users timeline
If you created your app on or after 2022-04-29, you won't be able to access streaming with Twitter API v1.1:
Additionally, beginning today, new client applications will not be able to gain access to v1.1 statuses/sample and v1.1 statuses/filter.
https://twittercommunity.com/t/deprecation-announcement-removing-compliance-messages-from-statuses-filter-and-retiring-statuses-sample-from-the-twitter-api-v1-1/170500
You'll have to use Twitter API v2 instead.
Tweepy's interface for streaming with Twitter API v2 is StreamingClient.

Learning to use Tweepy and Twitter API

I'm currently trying to learn the Twitter API within Python. My code is this:
import tweepy
consumer_key = "Consumer Key"
consumer_secret = "Consumer Secret"
access_token = "Access Token"
access_token_secret = "Access Token Secret"
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_acess_token(access_token, access_token_secret)
auth.secure = True
api = tweepy.API(auth)
tweet = "This tweet was made from a program"
api.update_status(status=tweet)
However this is the error that the code is giving me:
Forbidden: 403 Forbidden
453 - You currently have Essential access which includes access to Twitter API v2 endpoints only. If you need access to this endpoint, you’ll need to apply for Elevated access via the Developer Portal. You can learn more here: https://developer.twitter.com/en/docs/twitter-api/getting-started/about-twitter-api#v2-access-leve
Process finished with exit code 1
Do I really need to apply for further access just to tweet one thing? Thanks
Twitter has a newer (v2) API. You should use tweepy.Client to be able to use the new endpoints without signing up.

Get all users that are followed a particular Twitter user

I would like to get a list of all users a particular user is following. (Using a Python wrapper).
This question is similar to this one. But how do I do the same in Python ?
Your goal can be achieved using the Tweepy library for Python
step 1
Create a twitter developer account here
step 2
Install the tweepy library instructions available here
Example
# import tweepy library
import tweepy
# define your developer keys from https://developer.twitter.com/en
consumer_key='<your consumer_key here>'
consumer_secret='<your consumer_secret here>'
access_token='<your access_token here>'
acess_token_sectret='<your acess_token_sectret here>'
# use those defined keys to login to twitter using tweepy
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
# use tweepy to search for all the accounts that StackOverflow is following (to change which account it searches just change the username)
for user in tweepy.Cursor(api.friends, screen_name="StackOverflow").items():
# Print the username of each user who is following the account
print('following: ' + user.screen_name)
Note
Twitters rate limits for getting the followers of accounts is quite strict. You may need to wait a period of time between searches.

Django-social-auth and twitter update status

I want create application with django-social-auth. How can I get users possibility update twitter status?
I try to use tweepy, but it write me:
Invalid / expired Token
I use consumer_key and consumer_secret from my application, and oauth_token with oauth_token_secret from usersocialauth table in database.
auth = tweepy.OAuthHandler(settings.TWITTER_CONSUMER_KEY, settings.TWITTER_CONSUMER_SECRET)
auth.set_access_token(str(oauth_token), str(oauth_token_secret))
api = tweepy.API(auth)
api.update_status(TwiAge)
What I am doing wrong?

Categories