Twitter API:简单状态更新(Python)

Hof*_*off 2 python twitter oauth twitter-oauth

我一直在寻找一种从Python客户端更新我的Twitter状态的方法.根据http://dev.twitter.com/pages/oauth_single_token,由于此客户端只需要访问一个Twitter帐户,因此应该可以使用预先生成的oauth_token和secret来执行此操作.

但是示例代码似乎不起作用,我得到'无法验证您'或'错误签名'..

由于有很多不同的python-的Twitter库,在那里(而不是所有的人都跟上时代的)我会很感激,如果有人可以点我认为目前从事POST请求库,或发布一些示例代码!

更新: 我已经尝试了Pavel的解决方案,只要新消息只有一个字长就可以工作,但只要它包含空格,我就会收到此错误:

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
Run Code Online (Sandbox Code Playgroud)

但是,如果更新只是一个单词,它可以工作:

status = api.PostUpdate('helloworld')
{'status': 'helloworld'}
Run Code Online (Sandbox Code Playgroud)

知道为什么会这样吗?

非常感谢提前,

霍夫

Paw*_*żak 9

您可能对此http://code.google.com/p/python-twitter/感兴趣

不幸的是,这些文档并不公平,最后一次'发布'是在2009年.

我用过hg中的代码:

wget http://python-twitter.googlecode.com/hg/get_access_token.py
wget http://python-twitter.googlecode.com/hg/twitter.py
Run Code Online (Sandbox Code Playgroud)

在(长)应用程序注册过程(http://dev.twitter.com/pages/auth#register)之后,您应该拥有使用者密钥和密钥.它们是应用程序的独特之处.

接下来,您需要将应用与您的帐户连接,根据源(原文如此!)并运行指令编辑get_access_token.py.您现在应该拥有Twitter Access Token密钥和密钥.

>>> 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!
Run Code Online (Sandbox Code Playgroud)

它适用于我http://twitter.com/#!/pawelprazak/status/16504039403425792(不确定是否所有人都可以看到)

那说我必须补充一点,我不喜欢这些代码,所以如果我要使用它,我会重写它.

编辑:我已经让这个例子更加清晰.


Hof*_*off 7

我已经能够使用另一个库来解决这个问题 - 所以我会在这里发布我的解决方案以供参考:

import tweepy
# http://dev.twitter.com/apps/myappid
CONSUMER_KEY = 'my consumer key'
CONSUMER_SECRET = 'my consumer secret'
# http://dev.twitter.com/apps/myappid/my_token
ACCESS_TOKEN_KEY= 'my access token key'
ACCESS_TOKEN_SECRET= 'my access token secret'

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
Run Code Online (Sandbox Code Playgroud)