2011-05-28 37 views
1

當使用urllib2.urlopen嘗試從youtube gdata api提取數據時,我收到HTTP 403錯誤。爲了調試目的,我關閉了CSRF中間件,並且我使用的視圖如下所示:在從Django視圖中的gdata api提取數據時獲取HTTP 403

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
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)