Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter API: simple status update (Python)

I've been looking for a way to update my Twitter status from a Python client. As this client only needs to access one Twitter account, it should be possible to do this with a pre-generated oauth_token and secret, according to http://dev.twitter.com/pages/oauth_single_token

However the sample code does not seem to work, I'm getting 'could not authenticate you' or 'incorrect signature'..

As there are a bunch of different python-twitter library out there (and not all of them are up-to-date) I'd really appreciate if anybody could point me a library that's currently working for POST requests, or post some sample code!

Update: I've tried Pavel's solution, and it works as long as the new message is only one word long, but as soon as it contains spaces, i get this error:

status = api.PostUpdate('hello world')
Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "C:\Python26\lib\site-packages\python_twitter\twitter.py", line 2459, in PostUpdate
        self._CheckForTwitterError(data)
      File "C:\Python26\lib\site-packages\python_twitter\twitter.py", line 3394, in _CheckForTwitterErro
    r
        raise TwitterError(data['error'])
    python_twitter.twitter.TwitterError: Incorrect signature

If however the update is just one word, it works:

status = api.PostUpdate('helloworld')
{'status': 'helloworld'}

Any idea why this might be happening?

Thanks a lot in advance,

Hoff

like image 794
Hoff Avatar asked Dec 01 '25 08:12

Hoff


1 Answers

You might be interested in this http://code.google.com/p/python-twitter/

Unfortunately the docs don't exist to be fair and last 'release' was in 2009.

I've used code from the hg:

wget http://python-twitter.googlecode.com/hg/get_access_token.py
wget http://python-twitter.googlecode.com/hg/twitter.py

After (long) app registration process ( http://dev.twitter.com/pages/auth#register ) you should have the Consumer key and secret. They are unique for an app.

Next you need to connect the app with your account, edit the get_access_token.py according to instructions in source (sic!) and run. You should have now the Twitter Access Token key and secret.

>>> import twitter
>>> api = twitter.Api(consumer_key='consumer_key',
            consumer_secret='consumer_secret', access_token_key='access_token',
            access_token_secret='access_token_secret')
>>> status = api.PostUpdate('I love python-twitter!')
>>> print status.text
I love python-twitter!

And it works for me http://twitter.com/#!/pawelprazak/status/16504039403425792 (not sure if it's visible to everyone)

That said I must add that I don't like the code, so if I would gonna use it I'd rewrite it.

EDIT: I've made the example more clear.

like image 61
Paweł Prażak Avatar answered Dec 03 '25 22:12

Paweł Prażak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!