Twitter API:简单状态更新(Python)
-
11-10-2019 - |
题
我一直在寻找一种从Python客户端更新我的Twitter状态的方法。由于该客户只需要访问一个Twitter帐户,因此可以使用预先生成的Oauth_token和Secret进行此操作。 http://dev.twitter.com/pages/oauth_single_token
但是,示例代码似乎不起作用,我得到了“无法验证您”或“错误签名”。
由于那里有一堆不同的python-twitter库(并非所有人都是最新的)呢
更新:我已经尝试了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'}
知道为什么这可能会发生吗?
非常感谢
霍夫
解决方案 2
我已经能够使用另一个库解决此问题 - 因此,我将在此处发布解决方案以供参考:
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
其他提示
您可能对此感兴趣 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 )您应该有消费者密钥和秘密。它们对于应用程序是独一无二的。
接下来,您需要将应用程序与您的帐户联系起来,请根据源(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 Code Page指向您指出的。)
现在,您不再需要使用python-twitter随附的命令行工具来获取完整的访问令牌和秘密, https://dev.twitter.com 注册应用程序时,您会要求它们。
一旦拥有四个不同的凭证值,您要做的第一件事就是通过进行API测试来测试它们:
api = twitter.api(computer_key ='computer_key',commuter_secret ='compution_secret',access_token_key ='access_token',access_token_secret ='access_token_secret')
打印api.verifycredentials()
这将向您显示您的凭据是否有效。如果收到错误,下一步是传递debughttp = true to api()调用 - 这将导致打印所有HTTP对话,以便您可以看到Twitter错误消息。
凭据工作后,您可以尝试调用postupdate()甚至只是getTimeline()
我已经编码了一些与这个问题有关的事情。
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"