我想上传一个视频文件与PHP和YouTube数据Api v3.PHP文件应该
这样用户就不必手动登录。
其目标是让用户能够通过应用程序或网站将视频上传到YouTube,而无需先登录谷歌。
这是我正在使用的代码:
<?php
if (!file_exists(__DIR__ . '/youtubeapi/vendor/autoload.php')) {
throw new Exception(sprintf('Please run "composer require google/apiclient:~2.0" in "%s"', __DIR__));
}
require_once __DIR__ . '/youtubeapi/vendor/autoload.php';
putenv('GOOGLE_APPLICATION_CREDENTIALS=service_credentials.json');
$client = new Google_Client();
$client->setScopes([
'https://www.googleapis.com/auth/youtube',
'https://www.googleapis.com/auth/youtube.force-ssl',
'https://www.googleapis.com/auth/youtube.upload',
'https://www.googleapis.com/auth/youtubepartner-channel-audit',
'https://www.googleapis.com/auth/youtubepartner'
]);
$client->useApplicationDefaultCredentials();
// Define service object for making API requests.
$service = new Google_Service_YouTube($client);
// Define the $video object, which will be uploaded as the request body.
$video = new Google_Service_YouTube_Video($client);
// Add 'snippet' object to the $video object.
$videoSnippet = new Google_Service_YouTube_VideoSnippet($client);
$videoSnippet->setCategoryId('22');
$videoSnippet->setDescription('Description of uploaded video.');
$videoSnippet->setTitle('Test video upload.');
$video->setSnippet($videoSnippet);
// Add 'status' object to the $video object.
$videoStatus = new Google_Service_YouTube_VideoStatus($client);
$videoStatus->setPrivacyStatus('private');
$video->setStatus($videoStatus);
// TODO: For this request to work, you must replace "YOUR_FILE"
// with a pointer to the actual file you are uploading.
// The maximum file size for this operation is 128GB.
$response = $service->videos->insert(
'snippet,status',
$video,
array(
'data' => file_get_contents("big_buck_bunny.mp4"),
'mimeType' => 'video/*',
'uploadType' => 'multipart'
)
);
print_r($response);
?>
这就是我遇到的错误:
> Fatal error: Uncaught Google_Service_Exception: { "error": { "errors":
> [ { "domain": "youtube.header", "reason": "youtubeSignupRequired",
> "message": "Unauthorized", "locationType": "header", "location":
> "Authorization" } ], "code": 401, "message": "Unauthorized" } } in
> /homepages/37/d797893845/htdocs/development/sitesmedia/youtube/youtubeapi/src/Google/Http/REST.php:118
> Stack trace: #0
> /homepages/37/d797893845/htdocs/development/sitesmedia/youtube/youtubeapi/src/Google/Http/REST.php(94):
> Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response),
> Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #1
> /homepages/37/d797893845/htdocs/development/sitesmedia/youtube/youtubeapi/src/Google/Task/Runner.php(176):
> Google_Http_REST::doExecute(Object(GuzzleHttp\Client),
> Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #2
> /homepages/37/d797893845/htdocs/development/sitesmedia/youtube/youtubeapi/src/Google/Http/REST.php(58):
> Google_Task_Runner->run() #3 /homepages/37/d797893845/ in
> /homepages/37/d797893845/htdocs/development/sitesmedia/youtube/youtubeapi/src/Google/Http/REST.php
> on line 118
发布于 2019-11-18 18:56:38
看起来YouTube不支持服务帐户。
https://developers.google.com/youtube/v3/docs/errors
未经授权(401)
(401) youtubeSignupRequired此错误表示用户有一个未链接的谷歌帐户,这意味着用户有一个谷歌帐户,但没有YouTube通道。这些用户可以访问许多依赖于用户授权的功能,例如对视频进行分级或将视频添加到watch_later播放列表中。但是,作为一个例子,用户需要一个YouTube频道才能上传视频。拥有Gmail帐户或Android设备的用户肯定有Google帐户,但可能还没有将该Google帐户链接到YouTube频道。
如果尝试使用OAuth 2.0服务帐户流,通常会看到此错误。YouTube不支持服务帐户,如果您试图使用服务帐户进行身份验证,您将得到此错误。
Google博客文章介绍了Google支持,并更详细地讨论了YouTube错误。尽管博客文章解释了API 2.1版本的错误,但错误的含义仍然适用。
听起来您需要创建一个YouTube通道,然后以这种方式对请求进行身份验证。https://developers.google.com/youtube/registering_an_application
https://stackoverflow.com/questions/58920289
复制相似问题