PHP:通过Youtube API v3上传视频文件,该视频文件无需用户干预即可自动登录

我想用PHP和YouTube Data Api v3上传视频文件。 PHP文件应

  1. 自动登录YouTube数据API
  2. 上传视频文件

使用户不必手动登录。

目标是允许用户无需先登录Google即可通过应用或网站将视频上传到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),'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
zhipiao6365454 回答:PHP:通过Youtube API v3上传视频文件,该视频文件无需用户干预即可自动登录

YouTube似乎不支持服务帐户。

https://developers.google.com/youtube/v3/docs/errors

  

未经授权(401)youtubeSignupRequired 。此错误表示用户具有未关联的Google帐户,这意味着该用户具有   一个Google帐户,但没有YouTube频道。这样的用户可以   访问许多取决于用户授权的功能,例如   为视频评分或将视频添加到watch_later播放列表。但是,由于   例如,用户需要一个YouTube频道才能上传   一段录像。拥有Gmail帐户或Android设备的用户是   肯定拥有Google帐户,但可能尚未链接该帐户   Google帐户访问YouTube频道。

     

如果您尝试使用OAuth 2.0服务帐户流程,通常会看到此错误。 YouTube不支持服务帐户,如果您   尝试使用服务帐户进行身份验证,您将获得此信息   错误。

     

YouTube API博客文章还介绍了Google帐户支持   详细讨论youtubeSignupRequired错误。虽然   博客文章解释了API版本2.1的错误,   错误仍然适用。

听起来像您将必须创建一个YouTube频道,然后以这种方式对请求进行身份验证。 https://developers.google.com/youtube/registering_an_application

本文链接:https://www.f2er.com/3078865.html

大家都在问