在Django视图中从gdata api中提取数据时获取HTTP 403

时间:2011-05-28 19:56:12

标签: django youtube-api gdata-api

尝试使用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)
    ...

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

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请求标头。 在网址和标题中似乎都需要密钥,因此根据您之前的代码,请尝试以下操作:

req = urllib2.Request(youtube_search_url, encoded_params, { "X-GData-Key": '<developer key>' })
f = urllib2.urlopen(req)