YouTube API V3 - 403 Forbidden

问题描述:

更新:YouTube API V3 - 403 Forbidden

感谢易卜拉欣我现在认为更接近这个工作。

使用这里提供的(https://developers.google.com/youtube/partner/docs/v1/contentOwners/list)的例子中,我得到另一个Forbidden错误:

错误调用get googleapis ..../YouTube的/合作伙伴/ V1/contentOwners fetchMine =真:(403)禁止

...

从以下链接的API资源管理器中试用此项功能时,我可以获取YouTube CMS帐户的详细信息。 https://developers.google.com/youtube/partner/docs/v1/contentOwners/list

另请注意,“开发者帐户”与我们的“Youtube CMS帐户”相关联,并且是YouTube合作伙伴。


希望有人可以帮帮忙找出是什么导致了这种(403)禁止错误尝试获取来自YouTube CMS帐户频道当我得到。

这里有一些细节,我迄今所做的:

操作系统:Linux,语言:PHP 5

  1. 创建了一个项目(@ console.developers.google.com)
  2. 已启用YouTube数据API V3,YouTube分析和FreeBase API
  3. 为OAuth2创建服务帐户凭证
  4. 获取了google-api-php-client并安装到服务器中
  5. 创建了一个测试脚本认证服务帐户(请参见下面的完整的PHP代码)

获得的结果与这些试验试图获取渠道至今:

测试1)当我设置参数:minetrue,它不返回一个错误,当我去/youtube.com/channel/{channelID_returned}

测试2)当我设定的参数,它返回的通道不存在:forUsernameSomeYoutubeUsername,它返回用户的通道和c确认返回的频道ID是否正确并正常工作。

测试3)这是我真正想要实现,我已经设置managedByMetrue并且已经设置了onBehalfOfContentOwnerCMSContentOwner,它然后返回(403)禁止错误:

Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling .... etc.etc (403) Forbidden 

只是一个请注意:我尝试使用“Google oAuth Web应用程序”流从CMS帐户中获取频道,并且运行良好。


全码:

<?php 

ini_set('display_errors',1); 
// Initialize... 
$key_Path = '...omitted...'; 
set_include_path('...omitted...'); 

require_once 'Google/Client.php'; 
require_once 'Google/Service/YouTube.php'; 

// Setup Credentials 
$client_id = '...omitted...'; 
$service_account_name = ''...omitted...'; 
$key_file_location = $key_Path.''...omitted....p12'; 

$client = new Google_Client(); 
$client->setApplicationName("'...omitted..."); 

$youtube = new Google_Service_YouTube($client); 

/************************************************ 
If we have an access token, we can carry on. 
Otherwise, we'll get one with the help of an 
assertion credential. In other examples the list 
of scopes was managed by the Client, but here 
we have to list them manually. We also supply 
the service account 
************************************************/ 
$service_token = get_Configuration('GOOGLE_API' , 'TOKEN'); 
//$client->revokeToken($service_token); 

if (!empty($service_token)) { 
    $client->setAccessToken($service_token); 
} 
$key = file_get_contents($key_file_location); 
$cred = new Google_Auth_AssertionCredentials(
    $service_account_name, 
    array(
     'https://www.googleapis.com/auth/youtube', 
     'https://www.googleapis.com/auth/youtube.readonly', 
     'https://www.googleapis.com/auth/youtubepartner', 
     'https://www.googleapis.com/auth/youtubepartner-channel-audit' 
    ), 
    $key 
); 

$cred->sub = '...ommited...'; 
$client->setAssertionCredentials($cred); 
if($client->getAuth()->isAccessTokenExpired()) { 
    $client->getAuth()->refreshTokenWithAssertion($cred); 
} 

/************************************************ 
    We're just going to make the same call as in the 
    simple query as an example. 
************************************************/ 
$service_token = $client->getAccessToken(); 

$params = array(//'mine' => true, 
       //'forUsername' => ''...omitted...', 
       'managedByMe' => true, 
       'onBehalfOfContentOwner' => ''...omitted...' // CMS Account User ID 
       ); 

$channels = $youtube->channels->listChannels('contentDetails' , $params); 

print_r($channels); 
//$Channels = $youtube->Channels->list('contentDetails','{mine : true}'); 

?> 

在这里使用的服务帐户,你应该自己&管理通道和通道应与您正在试图获取您的CMS帐户。然后指定managedbyMe和内容所有者的ID(不是名称)。

更多信息:https://developers.google.com/youtube/v3/docs/channels/list#managedByMe

例子:https://developers.google.com/youtube/partner/code_samples/php

+0

感谢易卜拉欣您的建议,我已经做了,你在这里建议什么,但我可能会做一些错误的contentOwnerID部分。 contentOwner的ID是否必须是频道的实际所有者或管理频道的用户ID? – Joe 2014-10-06 15:05:39