Youtube API v3获取视频 - PHP

时间:2014-01-08 11:09:50

标签: php youtube google-api

我正在尝试在搜索特定关键字时获取YouTube视频列表 我尝试过以下教程:Search by keyword
不幸的是,它不起作用。

我的屏幕上出现以下错误:

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 Data API v3
  • YouTube Analytics API
  • Freebase API
  • 我错过了一些API吗? 我还启用了结算功能,但它仍无效。


    我的代码的一小部分:

        $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'],
    ));
    

    $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'], ));

    1 个答案:

    答案 0 :(得分: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)
    

    这将是工作