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

标签 php api youtube youtube-data-api

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

  • 自动登录到YouTube数据API
  • 上传视频文件

  • 这样用户就不必手动登录。

    目的是允许用户能够通过应用程序或网站将视频上传到YouTube,而无需先登录Google。

    这是我正在使用的代码:
    <?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
    

    最佳答案

    YouTube似乎不支持服务帐户。

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

    unauthorized (401) youtubeSignupRequired This error indicates that the user has an unlinked Google Account, which means that the user has a Google Account but does not have a YouTube channel. Such users can access many features that are dependent on user authorization, such as rating videos or adding videos to a watch_later playlist. However, as an example, the user would need a YouTube channel to be able to upload a video. A user who has a Gmail account or an Android device is certain to have a Google Account but may not have already linked that Google Account to a YouTube channel.

    This error is commonly seen if you try to use the OAuth 2.0 Service Account flow. YouTube does not support Service Accounts, and if you attempt to authenticate using a Service Account, you will get this error.

    The YouTube API blog post introducing Google Account support also discusses the youtubeSignupRequired error in more detail. Although the blog post explains the error for API version 2.1, the meaning of the error is still applicable.



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

    关于php - PHP:通过Youtube API v3上传视频文件,该视频文件无需用户干预即可自动登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58920289/

    相关文章:

    Android Activity 中的多个 YouTube 视频

    ios - YouTube-Player-iOS-Helper 不能使用 YTPlayerView 类

    java - REST-API 错误响应的不同内容类型

    python - 使用 Python 从 JSON API 中提取数据

    javascript - 如何在输入复选框中显示 php 变量

    javascript - 如何导入外部JSON并在php中显示

    javascript - 创建仪表板时重载构造函数

    javascript - 使用window.onbeforeunload暂停YouTube视频,使其不会与音频重叠

    php - 如何使用 php 分割长字符串并插入到 mysql 数据库中?

    菲律宾比索用户可以在文件中写入数据,我包含该文件