2017-09-02 131 views
1

如果我嘗試獲取某個頻道的所有視頻,我總是會收到此錯誤。YouTube API v3 - keyInvalid錯誤400 - PHP

{ "error": { "errors": [ { "domain": "usageLimits", "reason": "keyInvalid", "message": "Bad Request" } ], "code": 400, "message": "Bad Request" } } 

我的代碼如下

$api_key = "AIzaSyD...nR8"; 
stream_context_set_default(['http' => ['ignore_errors' => true]]); 
$source_videos = file_get_contents("https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId={UCuB3qjWes1E8t4yUmbP360Q}&maxResults=1&key={".$api_key."}"); 

echo $api_key; 
echo $source_videos; 

我創建了一個項目上https://console.developers.google.com/apis/dashboard並補充,激活了 「YouTube數據API V3」,創建的證書與「Web服務器(如Node.js的,Tomcat的)「作爲平臺,並最終在我的PHP文檔中複製密鑰。我不知道爲什麼我不能使用它。我感謝幫助。 :)

編輯:

使用瀏覽器上(JavaScript的)爲平臺,它應該工作。

+0

var_dump($ source_videos)的輸出是什麼? –

+0

上面的寫入錯誤 – Leschge

回答

0

使用Webbrowser(JavaScript)爲平臺,它應該工作。

0
try this code 
$API_key = 'AIzaSyCTPwxSs1id2kdv_fupUfbYkfa6Fucp_6A'; 
    $channelID = 'UCTCU28hEulL2jkafQ21uuWA'; 
    $maxResults = 10; 

    $videoList = json_decode(file_get_contents('https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId='.$channelID.'&maxResults='.$maxResults.'&key='.$API_key.'')); 

注意:您需要一個虛擬服務器來運行PHP(WAMP/MAMP/XAMPP)