尝试使用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)
...
有什么想法吗?
答案 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)