【发布时间】:2014-10-04 20:37:57
【问题描述】:
更新:
感谢 Ibrahim,我现在认为离实现这一目标更近了。
使用此处提供的示例 (https://developers.google.com/youtube/partner/docs/v1/contentOwners/list),我得到另一个 Forbidden 错误:
调用 GET googleapis 时出错..../youtube/partner/v1/contentOwners?fetchMine=true: (403) Forbidden
...
通过以下链接的 API Explorer 尝试此操作时,我能够获取 YouTube CMS 帐户详细信息。 https://developers.google.com/youtube/partner/docs/v1/contentOwners/list
另请注意,“开发者帐户”与我们的“Youtube CMS 帐户”相关联,并且是 YouTube 合作伙伴。
希望有人可以帮助找出导致我在尝试从 YouTube CMS 帐户获取频道时遇到的此 (403) 禁止错误的原因。
以下是一些细节以及我到目前为止所做的事情:
操作系统:Linux,语言:PHP 5
- 创建了一个项目 (@console.developers.google.com)
- 启用 YouTube Data API V3、YouTube Analytics 和 FreeBase API
- 为 OAuth2 创建了服务帐户凭据
- 获取 google-api-php-client 并安装到服务器中
- 创建了一个用于验证服务帐户的测试脚本(请参阅下面的完整 PHP 代码)
到目前为止,通过这些尝试获取频道的测试获得了结果:
测试1)当我将参数:mine设置为true时,它没有返回错误,当我去/youtube.com/channel/{channelID_returned}时它返回的频道不存在
测试2)当我将参数:forUsername设置为SomeYoutubeUsername时,它返回用户的频道,并确认返回的频道ID是正确的并且可以工作。
测试3)这是我真正想要完成的,我将managedByMe设置为true并将onBehalfOfContentOwner设置为CMSContentOwner,然后它返回(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}');
?>
【问题讨论】:
标签: google-api youtube-api google-api-php-client google-authentication