【问题标题】:python twitter 404蟒蛇推特404
【发布时间】:2013-02-01 06:57:59
【问题描述】:

我正在尝试使用 twitter-python 连接到 twitter 的 API,但我不断收到这个讨厌的 404 错误!

代码如下:

import sys
import twitter
def main():
  api = twitter.Api()
  statuses = api.GetPublicTimeline()
  print [s.user.name for s in statuses]

它应该返回在公共时间线上发布状态更新的人的姓名,但我得到的是这个回溯。

Traceback (most recent call last):
  File "new.py", line 14, in <module>
    main()
  File "new.py", line 10, in main
    statuses = api.GetPublicTimeline()
  File "/usr/lib/pymodules/python2.7/twitter.py", line 1319, in GetPublicTimeline
    json = self._FetchUrl(url,  parameters=parameters)
  File "/usr/lib/pymodules/python2.7/twitter.py", line 2030, in _FetchUrl
    url_data = opener.open(url, encoded_post_data).read()
  File "/usr/lib/python2.7/urllib2.py", line 400, in open
    response = meth(req, response)
  File "/usr/lib/python2.7/urllib2.py", line 513, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python2.7/urllib2.py", line 438, in error
    return self._call_chain(*args)
  File "/usr/lib/python2.7/urllib2.py", line 372, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 521, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 404: Not Found

如果我尝试进行身份验证,我的所有请求都会返回 401。

感谢您的帮助!

【问题讨论】:

    标签: python api twitter python-twitter


    【解决方案1】:

    https://code.google.com/p/python-twitter/ 他们说:

    python-twitter 库现在仅支持 oAuth 身份验证,因为 Twitter 开发人员表示 oAuth 是唯一支持的方法。

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

    这与您的问题有关吗?

    【讨论】:

    • 可能是这样。我对 oAuth 一无所知,所以我会对此进行一些研究,看看我能想出什么。
    • 我不确定,但 Twitter 最近做了一些更改,可能很多示例代码 out there 已过时且具有误导性。
    【解决方案2】:

    Twitter 是更好的 API 之一,可以让开发人员轻松完成 oAuth 流程。如果您登录 Twitter 并转到开发者仪表板https://dev.twitter.com/apps,您可以注册一个新应用程序(或配置您现有的应用程序)。

    查看应用程序详细信息将为您提供一个“oAuth 工具”选项卡,您可以在其中找到该应用程序的所有相关 oAuth 值:Consumer Key、Consumer Secret、Access Token 和 Access Token Secret。

    如果您要创建一个始终以同一用户身份通过​​ Twitter 进行身份验证的应用程序,您可以简单地将这些值硬编码到您的代码中,或者将它们存储在某个配置文件中。如果您的应用程序需要代表多个用户向 Twitter 进行身份验证(例如,以便它可以代表一个用户发布状态),那么您需要经历https://dev.twitter.com/docs/auth/3-legged-authorization 中描述的 oAuth 舞蹈。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-27
      • 2010-10-21
      • 2016-05-28
      • 2014-08-01
      • 2021-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多