I can't get Twython to update my twitter status - python

I've tried writing a simple program to update my twitter status using Twython(version 3). I am able to do a search of the public timeline, but it returns a "Twitter API returned a 401 (Unauthorized)" when I try to update the status.
from twython import Twython
APP_KEY = 'xxx'
APP_SECRET = 'xxx'
OAUTH_TOKEN = 'xxx'
OAUTH_TOKEN_SECRET = 'xxx'
twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
twitter.update_status(status='See how easy using Twython is!')
Does anyone have an idea what is wrong here?

You need to authenticate the user
twitter = Twython(APP_KEY, APP_SECRET)
auth = twitter.get_authentication_tokens(callback_url='http://mysite.com/callback')
Store the oauth tokens from the auth variable and store them somewhere you can retrieve them from when the user returns;
OAUTH_TOKEN = auth['oauth_token']
OAUTH_TOKEN_SECRET = auth['oauth_token_secret']
How you store it depends on your framework
redirect the user to auth url auth['auth_url']
and in the callback, you can retrieve the oauth sessions
Retrieve the oauth_verifier from the url params
Store the new tokens iin the sessionm, or wherever
twitter = Twython(APP_KEY, APP_SECRET,
OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
twitter_authroized=twitter.get_authorized_tokens(oauth_verifier)
OAUTH_TOKEN = twitter_authroized['oauth_token']
OAUTH_TOKEN_SECERT = twitter_authroized['oauth_token_secret']
Use these new OAUTH tokens from now on to make calls to twitter (instead of the old ones)
Read https://twython.readthedocs.org/en/latest/usage/starting_out.html#authentication for this information
https://github.com/ryanmcgrath/twython-django‎ is django project for twitter. Use it for guidance

Related

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.

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
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)

Twitter API Get User ID from Status ID

I am creating a Twitter bot that will follow the creator of a given status.
I have the status ID (tweet ID), but I need to grab the user ID of the user who posted the tweet in order to follow them. How can I get this? I am using the Twyton package.
You should use the request statuses/show/:id as specified in the Twitter REST API
In Twython, you should call show_status like this:
from twython import Twython
# define application keys here
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
twitter = Twython(consumer_key, consumer_secret, access_token, access_token_secret)
status = twitter.show_status(id='tweet_id')
print status['user']['id_str']

Twitter API 404 error with python twitter module

I am trying to get the list of friends of a user using the Python twitter module. For some reason I keep getting 404 errors. I bet I am missing something very basic. Any ideas?
Thanks!
PS: all OAuth parameters replaced with XXXX.
import twitter
CONSUMER_KEY = 'XXXX'
CONSUMER_SECRET = 'XXXX'
OAUTH_TOKEN = 'XXXX'
OAUTH_TOKEN_SECRET = 'XXXX'
auth = twitter.oauth.OAuth(OAUTH_TOKEN, OAUTH_TOKEN_SECRET,
CONSUMER_KEY, CONSUMER_SECRET)
twitter_api = twitter.Twitter(auth=auth)
print twitter_api.GetFriends(user='twitter')
Then I get the following error:
TwitterHTTPError: Twitter sent status 404 for URL: 1.1/GetFriends.json using parameters: (oauth_consumer_key=XXXX&oauth_nonce=XXXX&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1388098945&oauth_token=XXXX&oauth_version=1.0&user=twitter&oauth_signature=XXXX)
details: {"errors":[{"message":"Sorry, that page does not exist","code":34}]}
If you're using Python-twitter by DeWitt Clinton, then as PepperoniPizza mentioned, you have an error in your OAuth statement. Your code should be changed as follows:
auth = twitter.oauth.OAuth(OAUTH_TOKEN, OAUTH_TOKEN_SECRET,
CONSUMER_KEY, CONSUMER_SECRET)
Should become
auth = twitter.Api(consumer_key=CONSUMER_KEY, consumer_secret=CONSUMER_SECRET, access_token_key=OAUTH_TOKEN, access_token_secret=OAUTH_TOKEN_SECRET)
See here, under section "Using":
https://code.google.com/p/python-twitter/
However, looking at your code, your work doesn't seem to match how that library opens a connection to twitter. Which library are you using? It almost looks like you're using Mike Verdone's Python Twitter Tools.

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