PHP 中的 Youtube Data API v3 Account Service : 403 - 禁止 : Yout


Youtube Data API v3 Account Service in PHP : 403 - Forbidden : YoutubeSignupRequired

我为此砰砰��

我想做的是在YouTube上存储视频,用于我们正在举行的视频比赛。我真的很喜欢通过服务帐户进行 2 条腿的 OAuth 访问(我希望即使没有 gmail 帐户的用户也能向我们发送视频,我想将这些视频放在我们的 youtube 帐户中私密或未列出)。

但这似乎不起作用。我现在的代码是这样的:

function go()
{
require_once 'googleClient/Google_Client.php';
require_once 'googleClient/contrib/Google_YoutubeService.php';
define("CLIENT_ID",'xxxxxxx.apps.googleusercontent.com');
define("SERVICE_ACCOUNT_NAME",'xxxxxxx@developer.gserviceaccount.com');
define("KEY_FILE", 'googleClient/verylonghashtagprivacystuff-privatekey.p12');
define("SECRET_FILE", 'client_secrets.json');
$client = new Google_Client();
$client->setApplicationName("My Contest App");
$key = file_get_contents(KEY_FILE);
$secret = file_get_contents(SECRET_FILE);

$GAC = new Google_AssertionCredentials( SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/youtube.upload',
            'https://www.googleapis.com/auth/youtube'), 
$key ,'notasecret','http://oauth.net/grant_type/jwt/1.0/bearer');
$client->setAssertionCredentials($GAC);
$client::$auth->refreshTokenWithAssertion();
$client->setClientId(CLIENT_ID); // Did tried to put that one sooner
$client->setClientSecret($secret); // Did tried to put that one sooner
$json = $client->getAccessToken();
$accessToken = json_decode($json)->access_token;
$video = array();
$video["snippet"] = array();

$snippet = new Google_VideoSnippet();
$snippet->setTitle("Contest Video USERNAME");
$snippet->setDescription("Video for contest 2013 USERNAME.");
$snippet->setTags(array("Contest","Whatever","2013"));
$snippet->setCategoryId("22"); //Did tried "Entertainment".
$status = new Google_VideoStatus();
$status->setPrivacyStatus("unlisted");
$video = new Google_Video();
$video->setSnippet($snippet);
$video->setStatus($status);
$client->setAccessToken($json);
$youtube = new Google_YoutubeService($client);
$url = '/projet_video.mp4';
$response = $youtube->videos->insert(
"status,snippet", 
$video, 
array(  'data' => file_get_contents($url),'mimeType' => "video/*")); 
//Did tried video/mp4

return $response ;}

结果是:

error": {  "errors": [   {
    "domain": "youtube.header",
    "reason": "youtubeSignupRequired",
    "message": "Forbidden",
    "locationType": "header",
    "location": "Authorization"
}], "code": 403,  "message": "Forbidden" }

服务帐号不适用于 YouTube API

服务帐号不适用于 YouTube API 调用,因为关联了 YouTube 频道,您无法将新频道或现有频道与服务帐号相关联。使用服务帐号进行 YouTube API 调用将返回错误,错误类型设置为"未授权",原因设置为"youtubeSignupRequired"。

来源: https://developers.google.com/youtube/v3/guides/moving_to_oauth#service_accounts

在对

OP进行线程外跟进后,问题归结为各种YouTube API不支持服务帐户,至少目前不支持。您需要使用与您要访问的 YouTube 频道关联的实际 Google 帐号的凭据完成文档中描述的流程之一(不包括服务帐号流程)。

我解决了它,我的答案在这里:如何使用 ruby 通过服务帐户插入视频 youtube api v3 享受!

收到的错误是因为您没有在要将电影上传到的电子邮件中添加人员标签。如果您使用我的答案中的代码,这一切都已解决并正常工作