Twitter API 404 error with python twitter module - python

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.

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.

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.

Syntax error authenticating tweepy

I am quite new to installing modules on python. I'm trying to get started with Tweepy but I'm hitting an error.
I have run
import tweepy
auth = tweepy.OAuthHandler(MYCONSUMERKEY, MYCONSUMERSECRET)
But the following error is returned:
File "<stdin>", line 1
auth = tweepy.OAuthHandler(MYCONSUMERKEY, MYCONSUMERSECRET)
^
SyntaxError: invalid syntax
Any idea what could be going wrong here? Im using Python 2.7 on OSX El Capitan
I presume you have your Twitter Apps credentials (otherwise, nothing will work).
Use the following portion of code (assuming you have the required credentials)
import tweepy
# Fill this up with the Twitter Apps Credentials (get them # apps.twitter.com)
consumer_key = "Your consumer Key"
consumer_secret = "Your consumer Key Secret"
access_token = "Your access Token"
access_token_secret = "Your access Token Secret"
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
print "User Name:", api.me().name
print "Account (#):", api.me().screen_name
With this, you have the basic framework to start interacting with the Twitter API using Tweepy (the last two lines, based on your Twitter credentials, should print your name and #username in Twitter.

tweepy error response status code 400

I'm trying to write a simple twitter bot using python and tweepy. The code is as follows:
import tweepy
CONSUMER_KEY = 'xxxxxxxxxxxxxxxxx'
CONSUMER_SECRET = 'xxxxxxxxxxxxxxxxxxxxx'
ACCESS_KEY = 'xxxxxxxxxxxxxxxxxxxxx'
ACCESS_SECRET = 'xxxxxxxxxxxxxxxxx'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
api.update_status('hi')
I get the following error:
TweepError: Twitter error response: status code = 400
I would like some info on how to avoid this error
This may be a bug that emerged in the 3.2.0 release of Tweepy. See this issue opened on GitHub. In that issue, TylerGlaiel notes that api.update_status fails if called thus:
api.update_status('Test')
But works if called like so:
api.update_status(status='Test')
I have also found this works. I imagine a fix will be out before long.
This sentence: api.update_status(status='Test') let you know if your API Key and Token is working well. So, if you see:
TweepError: Twitter error response: status code = 400
Possibly means that you need to regenerate your credentiales for any reason. Regenerate your API Key, API Secret Key and your Tokens and will work.

“Incorrect Signature” Python Twitter

I have the following code:
import urlparse
import oauth2 as oauth
PROXY_DOMAIN = "twitter1-ewizardii.apigee.com"
consumer_key = '...'
consumer_secret = '...'
consumer = oauth.Consumer(consumer_key, consumer_secret)
oauth_token = '...'
oauth_token_secret = '...'
token = oauth.Token(oauth_token, oauth_token_secret)
client = oauth.Client(consumer, token)
request_token_url = "https://twitter1-ewizardii.apigee.com/1/account/rate_limit_status.json"
resp, content = client.request(request_token_url, "GET", PROXY_DOMAIN)
print resp
print content
However I continue to get the error "error":"Incorrect signature" this was working earlier, and I tried out solutions people have suggested online, generated new credentials etc, but it doesn't seem to work anymore after working for a week like this.
Thanks,
Although I have switched to tweepy for anyone who finds this question this may be of use to you:
http://dev.twitter.com/pages/libraries
It could have been a glitch on the day I was testing as I didn't go back to trying out the oauth-python module since tweepy has been working for me. But that link list all the possible libraries available and is a valuable resource if such a problem arises again.

Categories