How to Send a Tweet from Python? I can read, but not post.
saqib.ali.75 at gmail.com
saqib.ali.75 at gmail.com
Mon Feb 11 15:38:13 EST 2013
I posted this on StackOverflow.com but haven't got a good response yet: http://stackoverflow.com/questions/14808245/python-why-cant-i-send-a-tweet
I have tried two different packages python-twitter and tweepy but have not been successful using either package. Those solutions are discussed here:
Twitter API: simple status update (Python) How to tweet from Django?
Firstly, I should make clear that I have just created a Twitter account a couple of hours ago from which to send tweets. Using dev.twitter.com, I have set the access level to "Read, write, and direct messages" and I have created consumer and access token keys.
Using these keys, I connect to twitter using the python-twitter package. I know it is at least partially working because I can access The Biebs tweets:
>> CONSUMER_KEY = 'XXXX'
>> CONSUMER_SECRET = 'XXXX'
>> ACCESS_TOKEN_KEY= 'XXXX'
>> ACCESS_TOKEN_SECRET= 'XXXX'
>> import twitter
>> api = twitter.Api()
>> api = twitter.Api(
>> consumer_key=CONSUMER_KEY,
>> consumer_secret=CONSUMER_SECRET,
>> access_token_key=ACCESS_TOKEN_KEY,
>> access_token_secret=ACCESS_TOKEN_SECRET
>> )
>> statuses = api.GetUserTimeline("justinbieber")
>> for s in statuses:
>> print s.text
http://t.co/q58qksxp its not finished but heres a little part a song I'm working on
sorry http://t.co/Vtu4Lc2Q
video is at 13 percent done uploading
<... Other Inanities from The Bieb Snipped. You get the picture...>
However, when I try to send a tweet, I get the error: "Could not authenticate with OAuth."
>> api.PostUpdate('Testing Twitter!')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/python_twitter-0.8.5-py2.7.egg/twitter.py", line 2838, in PostUpdate
data = self._ParseAndCheckTwitter(json)
File "/usr/local/lib/python2.7/dist-packages/python_twitter-0.8.5-py2.7.egg/twitter.py", line 3869, in _ParseAndCheckTwitter
self._CheckForTwitterError(data)
File "/usr/local/lib/python2.7/dist-packages/python_twitter-0.8.5-py2.7.egg/twitter.py", line 3892, in _CheckForTwitterError
raise TwitterError(data['error'])
twitter.TwitterError: Could not authenticate with OAuth.
Can someone explain why? Since that didn't work, I decided to install and try tweepy.
But when I try to use it, it gives me the error "Invalid or expired token"
>> import tweepy
>> def tweet(status):
>> '''
>> updates the status of my twitter account
>> requires tweepy (https://github.com/joshthecoder/tweepy)
>> '''
>> if len(status) > 140:
>> raise Exception('status message is too long!')
>> auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
>> auth.set_access_token(ACCESS_TOKEN_KEY, ACCESS_TOKEN_SECRET)
>> api = tweepy.API(auth)
>> result = api.update_status(status)
>> return result
>>
>> result = tweet('Testing Twitter!')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 11, in tweet
File "/usr/local/lib/python2.7/dist-packages/tweepy-2.0-py2.7.egg/tweepy/binder.py", line 185, in _call
return method.execute()
File "/usr/local/lib/python2.7/dist-packages/tweepy-2.0-py2.7.egg/tweepy/binder.py", line 168, in execute
raise TweepError(error_msg, resp)
tweepy.error.TweepError: [{u'message': u'Invalid or expired token', u'code': 89}]
What is the solution here? Are my keys from Twitter wrong?
More information about the Python-list
mailing list