2

我一直在寻找一种从 Python 客户端更新我的 Twitter 状态的方法。根据http://dev.twitter.com/pages/oauth_single_token,由于此客户端只需要访问一个 Twitter 帐户,因此应该可以使用预先生成的 oauth_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'}

知道为什么会发生这种情况吗?

非常感谢提前,

霍夫

4

4 回答 4

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

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

接下来,您需要将应用程序与您的帐户连接,根据源代码(原文如此!)中的说明编辑 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(不确定它是否对每个人都可见)

也就是说,我必须补充一点,我不喜欢代码,所以如果我要使用它,我会重写它。

编辑:我已经让这个例子更清楚了。

于 2010-12-17T20:05:55.967 回答
8

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

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
于 2010-12-20T16:37:57.707 回答
2

Python-Twitter 文档的最新位置现在在 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')

打印 api.VerifyCredentials()

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

一旦凭据正常工作,您就可以尝试调用 PostUpdate() 甚至只是 GetTimeline()

于 2013-02-12T01:40:34.110 回答
1

我已经编写了一些与这个问题相关的东西。

import tweepy
consumer_key = Your_consumer_key
consumer_secret = Your_consumer_secret
access_token = Your_access_token
access_token_secret = Your_access_token_secret_key

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
single_tweet = 'hello world'
api.update_status(single_tweet)
print "successfully Updated"
于 2014-12-22T12:14:11.453 回答