I'm trying to use the Twitter API in Python. I got elevated access, set up my app in the Developer Portal, and authenticated OAuth 1.0.
When I write code I always receive the following error: Stream encountered HTTP error: 403. What am I doing wrong?
import tweepy
api_key = ""
api_key_secret = ""
access_token = ""
access_token_secret = ""
stream = tweepy.Stream(api_key,api_key_secret, access_token, access_token_secret)
stream.filter(track=["President"])```
You will need to use Twitter API v2 for this (Tweepy supports it, but you will need to update your code). Regardless of elevated access, after April 29 2022, new applications can no longer access v1.1 streaming.
New Twitter Developer Apps:
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
Also answered here.
Related
I am trying to pull tweets using Twitter Developer API V2. My access level is Elevated. This is how my code looks like:
import tweepy
consumerKey = "*********mc7K"
consumerSecret = "*******BZNB"
accessToken = "*****-****9eeT"
accessTokenSecret = "****sWAa"
bearerToken="****VRqn"
client = tweepy.Client(wait_on_rate_limit=True, bearer_token=bearerToken, consumer_key=consumerKey, consumer_secret=consumerSecret, access_token=accessToken, access_token_secret=accessTokenSecret)
tweets = client.search_all_tweets(query=search_query, tweet_fields=['context_annotations', 'created_at'], max_results=10)
This throws me an error saying:
When authenticating requests to the Twitter API v2 endpoints, you must
use keys and tokens from a Twitter developer App that is attached to a
Project. You can create a project via the developer portal.
But if I use tweets = client.search_recent_tweets(query=search_query, tweet_fields=['context_annotations', 'created_at']), it works fine.
What am I missing here?
Tweepy - TWITTER API V2 REFERENCE
Client.search_all_tweets
This endpoint is only available to those users who have been approved
for the Academic Research product track.
The full-archive search endpoint returns the complete history of
public Tweets matching a search query; since the first Tweet was
created March 26, 2006.
The Tweets returned by this endpoint count towards the Project-level Tweet cap.
Here is the context of what I want to do:
Automate following certain profiles on Twitter with an account that is different from my Twitter developer account. For that I read that I need 3-legged OAuth.
Here is my code:
import tweepy
consumer_key = "XXX"
consumer_secret = "XXX"
oauth1_user_handler = tweepy.OAuth1UserHandler(
consumer_key, consumer_secret,
callback="callback url"
)
print(oauth1_user_handler.get_authorization_url(signin_with_twitter=True))
Following the link printed I am able to authenticate my app using the Twitter account and then get something like this:
https://my_callback_url?oauth_token=XXX&oauth_verifier=XXXX
Following Tweepy's documentation, I should be able to put this oauth_verifier there:
access_token, access_token_secret = oauth1_user_handler.get_access_token(
"Verifier (oauth_verifier) here"
)
However, it is not correct, because every time I run my code I need to authenticate again and get a new oauth_verifier token.
On the Twitter developer portal everything is setup with this OAuth 1 permission and putting a callback URL and website URL was mandatory. I don't know what else to do.
That's where I am stuck.
I am following Tweepy's documentation here
You should be able to reuse the access token and secret, not the verifier.
I have tried to authenticate Twitter API several times in different ways and I keep getting an authentication error. I have tried requesting the latest elevated version as well but I cannot get it to work.
This is my code:
import tweepy
# Variables that contains the credentials to access
Twitter API
ACCESS_TOKEN =
ACCESS_SECRET =
CONSUMER_KEY =
CONSUMER_SECRET =
# Setup access to API
def connect_to_twitter_OAuth():
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
api = tweepy.API(auth)
return api
# Create API object
api = connect_to_twitter_OAuth()
# tweets from my stream
public_tweets = api.home_timeline()
for tweet in public_tweets:
print(tweet.text)
This is the result:
TweepError: [{'message': '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', 'code': 453}]
I have tried the following tutorials but I cannot get the authentication to work. The tutorials did not mention elevated access and I'm not sure if there is a way to do this without:
https://gist.github.com/alexdeloy/fdb36ad251f70855d5d6
https://www.pythoncentral.io/introduction-to-tweepy-twitter-for-python/
https://towardsdatascience.com/my-first-twitter-app-1115a327349e
https://towardsdatascience.com/tweepy-for-beginners-24baf21f2c25
I have also looked at other stack overflow questions and answers and nothing seems to work.
I just want to get tweets from a specific user.
I have looked at all the Python Twitter API wrappers that I could find on Bitbucket, Github and PyPi, but have been unable to find one which allows you to connect to Twitter if you already have the authentication token.
I am aware that I can generate the authentication token using an OAuth token, OAuth token secret, Twitter Token and Twitter Secret; but I would like to skip that processing + not prompt users who already have accounts.
The tweepy library seems popular; but lacks documentation...
Would someone be able to show me a tweet postage which uses Tweepy (or any other Python Twitter library) that uses only the authentication token?
EDIT: I ended up getting to work right with Twython.
You have to store the access token and secret returned by the provider after authentication and use them in the subsequent requests to read or write. I have been using rauth (https://github.com/litl/rauth) and highly recommend it.
EDIT
Assuming you have already have a valid access token and a secret you can create a service object and read or write data using the twitter API (skipping the authentication steps). I have included the necessary steps from the rauth documentation below:
twitter = OAuth1Service(
name='twitter',
consumer_key='YOUR_CONSUMER_KEY',
consumer_secret='YOUR_CONSUMER_SECRET',
request_token_url='https://api.twitter.com/oauth/request_token',
access_token_url='https://api.twitter.com/oauth/access_token',
authorize_url='https://api.twitter.com/oauth/authorize',
header_auth=True)
params = {'include_rts': 1, # Include retweets
'count': 10} # 10 tweets
response = twitter.get('https://api.twitter.com/1/statuses/home_timeline.json',
params=params,
access_token=access_token,
access_token_secret=access_token_secret,
header_auth=True)
I'm attempting to connect to twitter using python, and I'm finding it really frustrating.
Everything I read suggests that I need a consumer key, a consumer secret, an access key and an access secret - for example: Using python OAUTH2 to access OAUTH protected resources
I can get the consumer key and the consumer secret from the twitter settings page for the little test app I created, but what about the other two? After a bit of googling it seems everyone thinks it's so obvious where you get this info from that it's not worth putting up, so I might be having a really dumb moment but could someone please spell it out for idiots like me please?
Edit:
OK to get these details open your app settings in Twitter and click the "My Access Token" link.
I suppose when looking for an Access Token, if you were to click on a link titled "My Access Token" might help. I'd love to attribute my stupidity to the wine, but really I don't know...
Almost all oauth examples on blogs seem to be examples of the authorisation phase of oauth and none focus on how to actually make requests once you have these, as once you understand how it works this part is quite obvious. Getting that initial understanding is quite difficult unfortunately.
If you're just trying access your twitter account from a script or app for yourself you can get the access token (called key in the python oauth library) and secret from dev.twitter.com at the bottom of the settings page for your app under the heading Your access token.
import oauth2 as oauth
import json
CONSUMER_KEY = "your app's consumer key"
CONSUMER_SECRET = "your app's consumer secret"
ACCESS_KEY = "your access token"
ACCESS_SECRET = "your access token secret"
consumer = oauth.Consumer(key=CONSUMER_KEY, secret=CONSUMER_SECRET)
access_token = oauth.Token(key=ACCESS_KEY, secret=ACCESS_SECRET)
client = oauth.Client(consumer, access_token)
timeline_endpoint = "https://api.twitter.com/1.1/statuses/home_timeline.json"
response, data = client.request(timeline_endpoint)
tweets = json.loads(data)
for tweet in tweets:
print tweet['text']
This example is using the python lib python-oauth2, which is an unfortunately named OAuth library not an OAuth2 library.
If you want to actually let other people authorise their account to be used by your app then you need to implement the redirect dance where you ask twitter for a request token/secret pair and then redirect the user to the twitter authorize page with this request token, they sign in and authorize the token and get redirected back to your application, you then exchange the request token for an access token and secret pair which you can store and use to make requests like above.
The Twitter Three-legged OAuth Example in the Readme at http://github.com/simplegeo/python-oauth2 seems to cover what needs to be done
Personally I use tweepy, it provides a nice python wrapper to Twitter's API