YouTube API - PHP中的公共访问密钥

时间:2014-06-19 08:05:24

标签: youtube-api google-api

如何在Youtube API v3中替换对公共API访问密钥的OAuth2访问权限?我应该使用setDeveloperKey函数吗?如果我这样做,如下所示它返回:

  

未配置访问权限。请使用Google Developers Console激活项目的API。

我已在Google控制台中设置了我的公共访问API,因此它应该没问题。

当我尝试通过OAuth访问时,它可以完美运行。

CORRECTED CODE:
  // Call set_include_path() as needed to point to your client library.
require_once 'Google/Client.php';
require_once 'Google/Service/YouTube.php';
session_start();

$API_KEY = 'AIzaSyB7HuVrtyiUV8ow1SegS6xxxxxxxxx';
$client = new Google_Client();
$client->setDeveloperKey($API_KEY);
$client->setScopes('https://www.googleapis.com/auth/youtube');
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
  FILTER_SANITIZE_URL);
$client->setRedirectUri($redirect);

// Define an object that will be used to make all API requests.
$youtube = new Google_Service_YouTube($client);
    // Call the channels.list method to retrieve information about the
    // currently authenticated user's channel.
    $channelsResponse = $youtube->channels->listChannels('contentDetails', array(
      'id' => 'UUJQ2_kh7m7muj2xxxx',
    ));

    $htmlBody = '';

      $playlistItemsResponse = $youtube->playlistItems->listPlaylistItems('snippet', array(
        'playlistId' => => 'UUJQ2_kh7m7muj2xxxx',
        'maxResults' => 50
      ));

      $htmlBody .= "";
      foreach ($playlistItemsResponse['items'] as $playlistItem) {
       $title =  $playlistItem['snippet']['title'];
       $video_id = $playlistItem['snippet']['resourceId']['videoId'];
          $htmlBody .= "<a href='https://www.youtube.com/watch?v=".$video_id."'>";
          $htmlBody .= "<img src='http://img.youtube.com/vi/".$video_id."/1.jpg'>".$title."</a></br>";
      }
      $htmlBody .= '</ul>';
?>

<!doctype html>
<meta charset="utf-8">
<html>
  <head>
    <title>My Uploads</title>
  </head>
  <body>
    <?=$htmlBody?>
  </body>
</html>

获取错误:

  

“domain”:“usageLimits”,       “reason”:“accessNotConfigured”,       “消息”:“未配置访问权限。请使用Google Developers Console激活项目的API。” }],“代码”:   403,“消息”:“访问未配置。请使用Google Developers   用于激活项目API的控制台。“

1 个答案:

答案 0 :(得分:0)

更改了您的代码。这对我有用。

<?PHP
 // (1): Choose a valid channel_id
 $myChannelId = 'UUJQ2_kh7m7muj2xxxx';              // 'REPLACE_ME';
 // (2): api version 3
 $API_KEY = 'AIzaSyB7HuVrtyiUV8ow1SegS6xxxxxxxxx';  // 'REPLACE_ME';

 // (3): Library from: https://github.com/google/google-api-php-client
 // set_include_path();  // REPLACE_ME as needed to point to your client library.
 require_once 'Google/Client.php';
 require_once 'Google/Service/YouTube.php';

session_start();

$client = new Google_Client();
$client->setDeveloperKey($API_KEY);
//$client->setScopes('https://www.googleapis.com/auth/youtube');
//$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
//  FILTER_SANITIZE_URL);
//$client->setRedirectUri($redirect);

// Define an object that will be used to make all API requests.
$youtube = new Google_Service_YouTube($client);
    // Call the channels.list method to retrieve information about the
    // currently authenticated user's channel.
    $channelsResponse = $youtube->channels->listChannels('contentDetails', array(
      'id' => $myChannelId,
    ));

    $htmlBody = '';
    foreach ($channelsResponse['items'] as $channel) {
      // Extract the unique playlist ID that identifies the list of videos
      // uploaded to the channel, and then call the playlistItems.list method
      // to retrieve that list.
      $uploadsListId = $channel['contentDetails']['relatedPlaylists']['uploads'];
      $playlistItemsResponse = $youtube->playlistItems->listPlaylistItems('snippet', array(
        'playlistId' => $uploadsListId,
        'maxResults' => 50
      ));
      $htmlBody .= " ";

      foreach ($playlistItemsResponse['items'] as $playlistItem) {
       $title =  $playlistItem['snippet']['title'];
       $video_id = $playlistItem['snippet']['resourceId']['videoId'];
       $htmlBody .= "<a href='https://www.youtube.com/watch?v=".$video_id."'>";
       $htmlBody .= "<img src='http://img.youtube.com/vi/".$video_id."/1.jpg'>".$title."</a></br>";
      }
      $htmlBody .= '</ul>';
      }
?>

<!doctype html>
<meta charset="utf-8">
<html>
  <head>
    <title>My Uploads</title>
  </head>
  <body>
    <?PHP print $htmlBody?>
  </body>
</html>
相关问题