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

2024-04-25 22:38:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在寻找从Python客户端更新Twitter状态的方法。由于这个客户机只需要访问一个Twitter帐户,因此应该可以使用预先生成的oauth_令牌和secret来实现这一点,根据http://dev.twitter.com/pages/oauth_single_token

但是示例代码似乎不起作用,我得到的是“无法验证您”或“不正确的签名”。。

由于有很多不同的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

但是,如果更新只是一个单词,则可以:

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

知道为什么会这样吗?

提前多谢了

霍夫


Tags: 代码inapi示例libstatuslinesite
3条回答

你可能对这个感兴趣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

在(长)个应用程序注册过程(http://dev.twitter.com/pages/auth#register)之后,您应该拥有使用者密钥和密钥。它们对于应用程序来说是独一无二的。

接下来,您需要将应用程序与您的帐户连接,根据source(sic!)中的说明编辑get_access_token.py然后跑。您现在应该拥有Twitter访问令牌密钥和机密。

>>> 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!

它对我很有用http://twitter.com/#!/pawelprazak/status/16504039403425792(不确定它是否对每个人都可见)

这意味着我必须加上我不喜欢代码,所以如果我想用它,我会重写它。

编辑:我已经把例子讲得更清楚了。

Python Twitter文档的最新位置现在在GitHub上(google代码页将您指向GitHub)

现在您不再需要使用python twitter附带的命令行工具来获取完整的访问令牌和机密,https://dev.twitter.com将允许您在注册应用程序时请求它们。

一旦有了四个不同的凭据值,首先要做的是通过进行API测试来测试它们:

api = twitter.Api(consumer_key='consumer_key', consumer_secret='consumer_secret', access_token_key='access_token', access_token_secret='access_token_secret')

print api.VerifyCredentials()

这将显示您的凭据是否有效。如果您得到一个错误,下一步是将debugHTTP=True传递给Api()调用-这将导致打印所有HTTP会话,以便您可以看到Twitter错误消息。

一旦凭据生效,就可以尝试调用PostUpdate()甚至GetTimeline()

我已经能够使用另一个库解决此问题-因此我将在此处发布我的解决方案以供参考:

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

相关问题 更多 >