Django-social-auth and twitter update status - python

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?

Related

How to send a Direct Message on twitter using Tweppy Client?

I am trying to a send a basic direct message on Twitter, but it isn't recognizing 'create_direct_message'. This is the code I am using:
Client.create_direct_message(participant_id = '129593148134547046', text = 'Hello')
This is the error message:
AttributeError: 'Client' object has no attribute 'create_direct_message'
This is how the tweepy website says how to do it on their website, so I'm not sure why my computer is not recognizing it. Is there some way to update tweepy? Could I be running an old version? Please help!
https://docs.tweepy.org/en/stable/client.html#manage-direct-messages
The following is steps to send a direct message using Tweepy.
On the Twitter developer portal you will need to upgrade your account to elevated.
Once elevated access is approved and create your app, then got user authentication settings.
Set it to the following:
Read and write and Direct message
Native App
Fill in Callback URL and website URL. (its not used by Tweepy, but must be filled)
Going back to the App view select the "Keys and Tokens" tab.
Store all the information but you need the following.
consumer_key
consumer_secret
access_token
access_token_secret
Once you have that information the following sample code should work (fill in the variables).
import tweepy
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
twitter = tweepy.API(auth)
recipient_id = '129593148134547046' # As per example above.
text = 'hello'
direct_message = api.send_direct_message(recipient_id, text)
print(direct_message.message_create['message_data']['text'])
If it works the print will be your message.

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.

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)

I can't get Twython to update my twitter status

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

Categories