2016-11-18 54 views
0

美好的一天,
我试图找回我通过我的YouTube频道所拥有的专用播放列表(和相关视频)throught“的OAuth 2.0服务器到服务器“;
但我只看到公共元素!
这是我的PHP代码:谷歌的OAuth 2.0服务器到服务器和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服务帐户的错误配置?

谢谢你的帮助!

回答

0

我认为在这种情况下,你可以使用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())); 
    }