2014-01-08 191 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) 
    

    这将是工作

    相关问题