【问题标题】:YouTube API V3 - 403 ForbiddenYouTube API V3 - 403 禁止
【发布时间】: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

  1. 创建了一个项目 (@console.developers.google.com)
  2. 启用 YouTube Data API V3、YouTube Analytics 和 FreeBase API
  3. 为 OAuth2 创建了服务帐户凭据
  4. 获取 google-api-php-client 并安装到服务器中
  5. 创建了一个用于验证服务帐户的测试脚本(请参阅下面的完整 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


    【解决方案1】:

    要在此处使用服务帐户,您应该拥有和管理频道,并且频道应该与您尝试获取的 CMS 帐户相关联。然后指定 managedbyMe 和 contentowner 的 ID(不是名称)。

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

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

    【讨论】:

    • 感谢 Ibrahim 的建议,我已经完成了您在此处的建议,但我可能在 contentOwnerID 部分做错了。 contentOwner 的 ID 必须是频道的实际所有者还是管理频道的用户 ID?
    猜你喜欢
    • 1970-01-01
    • 2013-01-05
    • 1970-01-01
    • 2019-10-19
    • 1970-01-01
    • 2015-04-19
    • 1970-01-01
    • 1970-01-01
    • 2015-01-23
    相关资源
    最近更新 更多