从 Django 视图中的 gdata api 提取数据时出现 HTTP 403

发布于 2024-11-10 06:45:07 字数 487 浏览 5 评论 0原文

当尝试使用 urllib2.urlopen 从 youtube gdata api 提取数据时,我收到 HTTP 403 错误。我出于调试目的关闭了 CSRF 中间件,我使用的视图如下所示:

def videos (request):
    params = {}
    youtube_search_url = 'http://gdata.youtube.com/feeds/api/videos'
    params['order_by'] = 'relevance'
    params['max_results'] = 10
    params['safeSearch'] = 'strict'
    params['v'] = 2
    params['key'] = '<developer key>'
    f = urllib2.urlopen(youtube_search_url, encoded_params)
    ...

有什么想法吗?

When trying to pull data from the youtube gdata api using urllib2.urlopen, I receive a HTTP 403 error. I've turned off the CSRF middleware for debugging purposes, and the view I'm using looks like this:

def videos (request):
    params = {}
    youtube_search_url = 'http://gdata.youtube.com/feeds/api/videos'
    params['order_by'] = 'relevance'
    params['max_results'] = 10
    params['safeSearch'] = 'strict'
    params['v'] = 2
    params['key'] = '<developer key>'
    f = urllib2.urlopen(youtube_search_url, encoded_params)
    ...

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等待圉鍢 2024-11-17 06:45:07
When you make an API request, use the X-GData-Key request header to specify your developer key as shown in the following example:

X-GData-Key: key=<developer_key>

Include the key query parameter in the request URL.

http://gdata.youtube.com/feeds/api/videos?q=SEARCH_TERM&key=DEVELOPER_KEY

^^ 直接从马嘴里说出来。您缺少 X-GData-Key 请求标头。
url 和 header 中似乎都需要该密钥,因此鉴于您之前的代码,请尝试以下操作:

req = urllib2.Request(youtube_search_url, encoded_params, { "X-GData-Key": '<developer key>' })
f = urllib2.urlopen(req)
When you make an API request, use the X-GData-Key request header to specify your developer key as shown in the following example:

X-GData-Key: key=<developer_key>

Include the key query parameter in the request URL.

http://gdata.youtube.com/feeds/api/videos?q=SEARCH_TERM&key=DEVELOPER_KEY

^^ Straight from the horse's mouth. You are missing the X-GData-Key request header.
The key seems to be required in both url and the header, so given your previous code try this:

req = urllib2.Request(youtube_search_url, encoded_params, { "X-GData-Key": '<developer key>' })
f = urllib2.urlopen(req)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文