【问题标题】:Google OAuth 2.0 for server-to-server and Youtube private playlists in PHPGoogle OAuth 2.0 用于 PHP 中的服务器到服务器和 Youtube 私人播放列表
【发布时间】:2017-04-02 02:00:06
【问题描述】:

美好的一天,
我尝试通过“OAuth 2.0 for server-to-server”检索我的 YouTube 频道拥有的私人播放列表(和相关视频);
但我只看到公共元素!
这是我的 PHP 代码:

$client = new Google_Client();
$client->setAuthConfig(__DIR__ . "/my_service_account.json"); 
$client->setScopes('https://www.googleapis.com/auth/youtube');
$client->setApplicationName("NameOfMyApp");
$client->refreshTokenWithAssertion();
$token = $client->getAccessToken();
$accessToken = $token['access_token'];
//---------
$YOUTUBE = new Google_Service_YouTube($client);
//---------
$arr_opt = array('channelId' => $MY_CHANNEL_ID );
$arr_playlist = $YOUTUBE->playlists->listPlaylists('snippet,contentDetails',$arr_opt);
var_export($arr_playlist);

我得到一个“正确”的对象, 但如果我的播放列表设置为“私有”,则“modelData”字段没有项目:

 'modelData' => 
 array (
   'pageInfo' => 
   array (
     'totalResults' => 0,
     'resultsPerPage' => 5,
   ),
   'items' => 
   array (
   ),
 ),

我忘记了什么?
这是 Youtube API 的限制?
通过我的 console.developers.google.com 对我的服务帐户进行错误配置?

感谢您的帮助!

【问题讨论】:

    标签: php oauth youtube oauth-2.0 youtube-data-api


    【解决方案1】:

    我认为在这种情况下你可以使用PlaylistItems: list。它应该为您获取所有包含更多信息的数据。

    您的代码也可能如下所示:

    if ($client->getAccessToken()) {
      try {
        // Call the channels.list method to retrieve information about the
        // currently authenticated user's channel.
        $channelsResponse = $YOUTUBE->channels->listChannels('contentDetails', array(
          'mine' => 'true',
        ));
    
        $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 .= "<h3>Videos in list $uploadsListId</h3><ul>";
          foreach ($playlistItemsResponse['items'] as $playlistItem) {
            $htmlBody .= sprintf('<li>%s (%s)</li>', $playlistItem['snippet']['title'],
              $playlistItem['snippet']['resourceId']['videoId']);
          }
          $htmlBody .= '</ul>';
        }
      } catch (Google_Service_Exception $e) {
        $htmlBody = sprintf('<p>A service error occurred: <code>%s</code></p>',
          htmlspecialchars($e->getMessage()));
      } catch (Google_Exception $e) {
        $htmlBody = sprintf('<p>An client error occurred: <code>%s</code></p>',
          htmlspecialchars($e->getMessage()));
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-18
      • 2015-07-24
      • 1970-01-01
      • 2013-08-26
      • 2013-10-24
      • 2014-07-26
      相关资源
      最近更新 更多