I have python code that posts tweets to twitter using oauth2 and that works fine but I need to attach some images to tweets and it seems that has to be done with api v1.1
On the twitter dev portal under project settings, user authentication it says "oAuth 1.0a and OAuth 2.0 turned on" and the Oauth 1.0a settings app permissions are read and write. I'm confused about which keys I should be using for Oauth 1.0a as when I use the consumer keys like this
my_api_key="xxx"
my_api_secret="xxx"
auth = tw.OAuthHandler(my_api_key, my_api_secret)
api = tw.API(auth, wait_on_rate_limit=True)
tweet = api.update_status("Testing tweepy API v1")
I get the error 220 - Your credentials do not allow access to this resource.
If I copy the access keys from the OAuth2 code that works then I get this error
32 - Could not authenticate you.
The OAuth2 code that works looks like this:
api = tweepy.Client(bearer_token=keys.twitter_bearer,
access_token=keys.access_key,
access_token_secret=keys.access_secret,
consumer_key=keys.consumer_key,
consumer_secret=keys.consumer_secret_key)
api.create_tweet(text=str)
I'm not sure what I'm doing wrong.
edit - am elevated
Went back to basics and investigated the code as everything else sounds right.
Turns out I was just doing it wrong.
This works:
auth = tweepy.OAuth1UserHandler(
consumer_key, consumer_secret, access_token, access_token_secret
)
api = tweepy.API(auth)
image=".\dataframe.png"
media=api.media_upload(image)
try:
api.update_status("Test",media_ids=[media.media_id])
except Exception as e:
print(e.class)
print(f"Exception occured - {e}")
else:
print("Success")
Related
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.
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.
I need my Twitter app to tweet some information, but something is going wrong.
First, I created an app and tried this code to test credentials:
auth = tweepy.OAuthHandler("CONSUMER_KEY", "CONSUMER_SECRET")
auth.set_access_token("ACCESS_TOKEN", "ACCESS_SECRET")
api = tweepy.API(auth)
try:
api.verify_credentials()
print("Authentication Successful")
except:
print("Authentication Error")
And got "Authentication Error".
Then I tried to write a tweet directly using
client = tweepy.Client(bearer_token, consumer_key, consumer_secret, access_token, access_token_secret)
client.create_tweet(text="********")
And now I got "tweepy.errors.Forbidden: 403 Forbidden" What should I do?
It's impossible to determine what exactly is happening in the first instance, since you're suppressing the actual error and printing that on any exception. You'll need to provide the full traceback for anyone to know what's going on with it.
However, if you have Essential access to the Twitter API, you won't be able to use Twitter API v1.1, and you'll encounter a 403 Forbidden error.
See the FAQ section about that in Tweepy's documentation for more information.
For the latter error, make sure your app has the write permission.
See the FAQ section about that in Tweepy's documentation for more information.
Do you want to post a tweet via V2? here is the solution I just answered to myself!
Install tweepy, then do as I do to tweet "Yeah boy! I did it".
!pip3 install tweepy --upgrade # to install and upgrade tweepy if you didn't.
Then make your BEARER, CONSUMER_KEY, CONSUMER_SECRET, ACCESS_KEY, and ACCESS_SECRET ready. If you don't know how to find them you should check Developer Platform -> Developer Portal -> Projects & Apps -> click on your project -> then look for "Keys and tokens"
import tweepy
client = tweepy.Client(bearer_token=BEARER, consumer_key=CONSUMER_KEY, consumer_secret=CONSUMER_SECRET, access_token=ACCESS_KEY, access_token_secret=ACCESS_SECRET)
client.create_tweet(text="Yeah boy! I did it")
This worked for me 100% tested. I still don't know if I can quote or reply to a tweet with V2 or not.
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 am trying to connect to the twitter stream api to fetch tweets in realtime. This piece of code was working till 5-6 ago. Suddenly I've started receiving 401 all the time. Strangely this is both happening in my local machine and on our production server which is located in cloud, so I think this is not a network related issue.
Here is the code:
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
auth.callback = 'https://localhost' #no effect commented/uncommented
stream = Stream(auth, l)
stream.filter(track=['twitter'], languages=["es"])
So far I've tried the following:
Generating new consumer key, consumer secret, access token and access token secret. I've used 4 different set of keys which was working previously.
Created a fresh new app from apps.twitter.com. Filled the callback url as some users reported that 401 "Authorization Required" is related with the callback URL field missing in the application page of twitter.
Synced my clock with an ntp server, both on my local and on production server. My clock is not off.
My application have the correct privileges and I received access token and secret after the configuration.
Here is the request:
'Content-Length': '22'
'Content-Type': 'application/x-www-form-urlencoded'
'Authorization': 'OAuth oauth_nonce="161484371946745487981492681844"
oauth_timestamp="1492681844"
oauth_version="1.0"
oauth_signature_method="HMAC-SHA1"
oauth_consumer_key="<oauth_consumer_key>"
oauth_token="<oauth_token>"
oauth_signature="tphQqFWaBvEo2byjZ%2BRqNAM30I0%3D"'
Method: 'POST'
URL: 'https://stream.twitter.com/1.1/statuses/filter.json?delimited=length'
Any help would be appreciated on why I am getting a 401 Unauthorized "Need Authorization" response.
Edit: I've also tried using Twython and I got the same response back from twitter.
Thanks
Maybe you can try using Twython OAuth2 to connect if you don't need to tweet back
Twython Auth
Or try using this code.
# Put the consumer keys
auth = tweepy.OAuthHandler("consumer_key", "consumer_secret")
# Redirect user to Twitter to authorize
redirect_user(auth.get_authorization_url())
# Get access token
auth.get_access_token("verifier_value")
# Construct the API instance
api = tweepy.API(auth)
#Create the stream
streamClass= StdOutListener()
stream= tweepy.Stream(auth = api.auth, listener=streamClass)
This should work.
Maybe your bot is not authorised on the account and this may be the error. But I don't think it's the case.
Check out my BOT's auth if you want an example :
ED_Postcards BOT
(New version incoming with pretty code)
I was able to solve the issue after 2 weeks of troubleshooting. For tweepy you need to manually set the callback url of the auth object you use to authenticate with the twitter.
After this correction head to apps.twitter.com and uncheck the box "Allow this application to to be used to Sign in with Twitter". Twitter API is not concise at all with the error messages.