Python Twitter API GET 请求

标签 python python-2.7 twitter get

我正在尝试使用 twitter package 和以下代码从 Twitter 中抓取一些推文:

>>> CONSUMER_KEY = 'xxxxxxxxxxxxxxxxxxxxxxx'
>>> CONSUMER_SECRET = 'xxxxxxxxxxxxxxxxxxxxxxx'
>>> OAUTH_TOKEN = 'xxxxxxxxxxxxxxxxxxxxxxx'
>>> OAUTH_TOKEN_SECRET = 'xxxxxxxxxxxxxxxxxxxxxxx'
>>> auth = twitter.oauth.OAuth(OAUTH_TOKEN_SECRET, OAUTH_TOKEN, CONSUMER_SECRET, CONSUMER_KEY)
>>> twitter_api = twitter.Twitter(auth=auth)
>>> print twitter_api
<twitter.api.Twitter object at 0x1085a7550>
>>> q = '#xxxxxxx'
>>> count = 100
>>> search_results = twitter_api.search.tweets(q=q, count=count)

在上面的最后一行之后,我得到了一堆行,最后一行详细说明了错误状态:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/twitter/api.py", line 204, in __call__
    return self._handle_response(req, uri, arg_data, _timeout)
  File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/twitter/api.py", line 235, in _handle_response
    raise TwitterHTTPError(e, uri, self.format, arg_data)
twitter.api.TwitterHTTPError: Twitter sent status 401 for URL: 1.1/search/tweets.json using parameters: (count=100&oauth_consumer_key=XXXXXXXXX&oauth_nonce=18110930768465666531&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1383724916&oauth_token=XXXXXXXXX&oauth_version=1.0&q=%23IdolsSA&oauth_signature=JGsmTXBIAHsX6Tq6bKjLPww%2B9SE%3D)
details: {"errors":[{"message":"Invalid or expired token","code":89}]}

我尝试生成新的消费者和 oauth 代码并输入它们,但仍然遇到相同的错误。我正在考虑尝试将代码放入新的 virtualenv 中。

我可能做错了什么?

谢谢

最佳答案

我认为您更改了默认参数顺序。文档说:

t = Twitter(
        auth=OAuth(OAUTH_TOKEN, OAUTH_SECRET,
                   CONSUMER_KEY, CONSUMER_SECRET))

尝试更改参数的顺序或使用关键字参数。

关于Python Twitter API GET 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19807366/

相关文章:

api - 推特 API : allow authenticated user to post tweets on another user's behalf

Python - Tweepy - 如何使用lookup_friendships?

python - Tweepy 空白搜索

python - 修改csv.writer代码,使其不留空格

python - 带颜色条的圆图

python - Pandas:合并两个一维数据帧,输出两列并填充唯一元素的值

python - 如何使全局变量从数据库中刷新

python - 将字典中列表的值转换为大写/小写

Python 2.X 在字符串周围添加单引号

python - ENIGMA 催化剂 - 警告 : Loader: Refusing to download new treasury data because a download succeeded