在IP[y] Notebook中调用WOE ID的趋势数据时出现Twitter API 401错误

0 投票
2 回答
937 浏览
提问于 2025-04-18 05:17

我正在按照《Mining the Social Web 2nd Edition》第一章的步骤操作,但在调用Yahoo GeoPlanet的WOE ID系统来获取趋势时,总是遇到错误。OAuth的信息已经用'*'遮住了。Github上的资料对我帮助不大。

顺便说一下,我最后不得不使用

from twitter import Twitter

而不是GitHub上的代码。不知道为什么,import Twitter(和import twitter)对我来说都不管用。我尝试过用pip安装,但还是不得不更改库的调用。

最初的认证似乎没问题:

from twitter import Twitter
consumer_key = '*****'
consumer_secret = '*****'
oauth_token = '*****'
oauth_secret = '*****'

auth = twitter.oauth.OAuth(oauth_token, oauth_secret, consumer_key, consumer_secret)

twitter_api = twitter.Twitter(auth=auth)

print twitter_api

<twitter.api.Twitter object at 0x106******>

但是调用API时却失败了:

WORLD_WOE_ID = 1
US_WOE_ID = 23424977

world_trends = twitter_api.trends.place(_id=WORLD_WOE_ID)
us_trends = twitter_api.trends.place(_id=US_WOE_ID)

print world_trends
print
print us_trends

TwitterHTTPError                          Traceback (most recent call last)
<ipython-input-29-e2835459a5aa> in <module>()
     10 # to the URL itself as a special case keyword argument.
     11 
---> 12 world_trends = twitter_api.Twitter.trends.place(_id=WORLD_WOE_ID)
     13 us_trends = twitter_api.Twitter.trends.place(_id=US_WOE_ID)
     14 

/Users/User/anaconda/lib/python2.7/site-packages/twitter/api.pyc in __call__(self, **kwargs)
    243 
    244         req = urllib_request.Request(uriBase, body, headers)
--> 245         return self._handle_response(req, uri, arg_data, _timeout)
    246 
    247     def _handle_response(self, req, uri, arg_data, _timeout=None):

/Users/User/anaconda/lib/python2.7/site-packages/twitter/api.pyc in _handle_response(self, req, uri, arg_data, _timeout)
    274                 return []
    275             else:
--> 276                 raise TwitterHTTPError(e, uri, self.format, arg_data)
    277 
    278 

TwitterHTTPError: Twitter sent status 401 for URL: 1.1/Twitter/trends/place.json using parameters: 
(id=1&oauth_consumer_key=*****&oauth_nonce=*****&oauth_signature_method=HMAC-SHA1&oauth_timestamp=*****&oauth_token=*****&oauth_version=1.0&oauth_signature=*****)
details:{"errors":[{"message":"Could not authenticate you","code":32}]}

我为什么能成功认证,却无法进行调用呢?

2 个回答

0

创建Twitter对象并不会让你自动登录,它只是把你的账号信息存起来,直到你真的用这些信息去调用API的时候,才会进行登录验证。也就是说,系统不会检查你的身份,直到你遇到错误为止。

请仔细检查你的OAuth账号信息是否正确,并确保你的系统日期和时间设置正确,因为如果时间差得太多,可能会导致连接问题。你可以通过登录https://apps.twitter.com,在你应用的API密钥页面的“你的访问令牌”部分,重新生成一个OAuth令牌。

0

用户出错了。这很常见。其实我应该按照书里的步骤来做,但我却偏离了轨道,手动安装了IPython Notebook,还试图自己运行一些东西。如果你正在阅读这本书,一定要仔细按照以下步骤来做:

  1. 安装VirtualBox
  2. 安装Vagrant
  3. 复制《Mining the Social Web 第二版》的GitHub仓库
  4. 使用git init {复制的git地址}克隆到你的电脑
  5. 在书的文件夹里运行'Vagrant Up'(这个过程可能需要一些时间)
  6. 通过 http://localhost:8888 打开IP[y] Notebook
  7. 进入第一章 - 矿工Twitter
  8. 使用书的作者(Matthew A. Russell)提供的代码
  9. 获取Twitter API密钥
  10. 直接使用

至于我为什么不能手动输入这些数据,我现在还不太确定。不过,很可能是因为使用IP[y] Notebook需要一个非常特定的安装过程,以确保ipython及其相关库被正确安装并可用。这种情况特别可能,因为我现在不再遇到调用'twitter'库/包的问题了。

《Mining the Social Web, 第二版》的
GitHub仓库链接: https://github.com/ptwobrussell/Mining-the-Social-Web-2nd-Edition
Vimeo频道(非常有帮助): http://vimeo.com/channels/MiningTheSocialWeb

希望这能帮助到其他人。

撰写回答