2014-01-08 197 views
2

我試圖在搜索特定關鍵字時獲取YouTube視頻列表。
我試過以下教程:Search by keyword
不幸的是它不起作用。
Youtube API v3獲取視頻 - PHP

 
A service error occurred: Error calling GET https://www.googleapis.com/youtube/v3/search?part=id%2Csnippet&q=amsterdam&maxResults=20&key=PRIVATEKEY: (403) Access Not Configured. Please use Google Developers Console to activate the API for your project. 


我已經激活了以下API在我的控制檯

  • YouTube數據API V3
  • 的YouTube Analytics(分析)API
  • 遊離鹼的API:我的屏幕上出現

    以下錯誤

  • 我是否缺少一些A PI的?
    我也啓用了結算功能,但仍然無法使用。


    我的代碼一小部分:

    $client = new Google_Client(); 
        $client->setDeveloperKey($DEVELOPER_KEY); 
    
           $youtube = new Google_YouTubeService($client); 
        $searchResponse = $youtube->search->listSearch('id,snippet', array(
            'q'   => $_GET['q'], 
            'maxResults' => $_GET['maxResults'], 
    )); 
    

    +0

    我已經解決了這個問題,我的引薦URI的地方可能是無效的。在我的谷歌控制檯中刪除了uri,現在允許所有的URI和它的工作。 – Robert

    回答

    1
    $DEVELOPER_KEY = ''; 
    
    $client = new Google_Client(); 
    $client->setDeveloperKey($DEVELOPER_KEY); 
    
    $youtube = new Google_YoutubeService($client); 
    
    try { 
        $searchResponse = $youtube->search->listSearch('id,snippet', array(
        'q' => $_GET['q'], 
        'maxResults' => $_GET['maxResults'], 
        )); 
    
    $videos = ''; 
    $channels = ''; 
    $playlists = ''; 
    
    foreach ($searchResponse['items'] as $searchResult) { 
        switch ($searchResult['id']['kind']) { 
        case 'youtube#video': 
         $videos .= sprintf('<li>%s (%s)</li>', $searchResult['snippet']['title'], 
         $searchResult['id']['videoId']); 
         break; 
        case 'youtube#channel': 
         $channels .= sprintf('<li>%s (%s)</li>', $searchResult['snippet']['title'], 
         $searchResult['id']['channelId']); 
         break; 
        case 'youtube#playlist': 
         $playlists .= sprintf('<li>%s (%s)</li>', $searchResult['snippet']['title'], 
         $searchResult['id']['playlistId']); 
         break; 
        } 
    } 
    
    
    echo ($playlists) 
    

    這將是工作

    相關問題