PHP Youtube API v3-直接上载-未经授权的消息

PHP Youtube API v3-直接上载-未经授权的消息,php,youtube,google-api-php-client,Php,Youtube,Google Api Php Client,我正试图直接上传视频到Youtube与API v3 我使用的是服务帐户场景(),我解决了google api php客户端库中的一些问题(读取p12文件并避免isAccessTokenExpired始终返回false) 没有“已创建”、“刷新令牌”和“标识令牌”字段。因此,我修复了Google_OAuth2类中的setAccessToken方法,如果未设置,则将“created”字段设置为time()。否则,isAccessTokenExpired始终返回false 现在,让我们上传文件

我正试图直接上传视频到Youtube与API v3

我使用的是服务帐户场景(),我解决了google api php客户端库中的一些问题(读取p12文件并避免isAccessTokenExpired始终返回false)

没有“已创建”、“刷新令牌”和“标识令牌”字段。因此,我修复了Google_OAuth2类中的setAccessToken方法,如果未设置,则将“created”字段设置为time()。否则,isAccessTokenExpired始终返回false

现在,让我们上传文件

    try{
        // Client init 
        $client = new Google_Client();
        $client->setClientId($client_id);
        $client->setClientSecret($client_secret);
        $client->setApplicationName($applicationName);

        $client->setAccessToken($result);

        if ($client->getAccessToken()) {

            if($client->isAccessTokenExpired()) {
                // @TODO Log error 
                echo 'Access Token Expired!!<br/>'; // Debug
            }

            $youtube = new Google_YoutubeService($client);

            $videoPath = "./test.mp4";

            // Create a snipet with title, description, tags and category id
            $snippet = new Google_VideoSnippet();
            $snippet->setTitle("fmgonzalez test " . time());
            $snippet->setDescription("fmgonzalez test " . time() );
            $snippet->setTags(array("tag1", "tag2"));

            // Numeric video category. See
            // https://developers.google.com/youtube/v3/docs/videoCategories/list
            $snippet->setCategoryId("22");

            // Create a video status with privacy status. Options are "public", "private" and "unlisted".
            $status = new Google_VideoStatus();
            $status->privacyStatus = "public";

            // Create a YouTube video with snippet and status
            $video = new Google_Video();
            $video->setSnippet($snippet);
            $video->setStatus($status);

            // Size of each chunk of data in bytes. Setting it higher leads faster upload (less chunks,
            // for reliable connections). Setting it lower leads better recovery (fine-grained chunks)
            $chunkSizeBytes = 1 * 1024 * 1024;

            // Create a MediaFileUpload with resumable uploads
            $media = new Google_MediaFileUpload('video/*', null, true, $chunkSizeBytes);
            $media->setFileSize(filesize($videoPath));

            // Create a video insert request
            $insertResponse = $youtube->videos->insert("status,snippet", $video,
                array('mediaUpload' => $media));

            $uploadStatus = false;

            // Read file and upload chunk by chunk
            $handle = fopen($videoPath, "rb");
            $cont = 1;
            while (!$uploadStatus && !feof($handle)) {
                $chunk = fread($handle, $chunkSizeBytes);
                $uploadStatus = $media->nextChunk($insertResponse, $chunk);
                echo 'Chunk ' . $cont . ' uploaded <br/>';
                $cont++;
            }

            fclose($handle);

            echo '<br/>OK<br/>';

        }else{
            // @TODO Log error 
            echo 'Problems creating the client';
        }

    } catch(Google_ServiceException $e) {
        print "Caught Google service Exception ".$e->getCode(). " message is ".$e->getMessage(). " <br>";
        print "Stack trace is ".$e->getTraceAsString();
    }catch (Exception $e) {
        echo $e->getMessage();
    }
我找到了关于其他场景的例子,但没有关于这个场景的

我应该怎么做才能最终上传视频文件?有关于这个场景的例子吗


提前谢谢

这可能看起来微不足道,但您是否在要将视频上载到的帐户中至少创建了一个频道。我使用了与您几乎相同的解决办法来处理acceess_令牌,然后遇到了相同的问题,直到我进入Youtube帐户上载部分,看到消息在上载视频之前创建了至少一个频道。希望有帮助。

您得到的错误响应是经过设计的。谷歌不允许通过服务帐户访问Youtube API

服务帐户不适用于YouTube数据API调用,因为 服务帐户需要关联的YouTube频道,而您不能 将新的或现有的渠道与服务帐户关联。如果你使用 服务帐户调用YouTube数据API,API服务器返回 错误类型设置为未经授权的错误,原因设置为 youtubeSignupRequired


我独立地找到了您的确切位置,必须添加相同的令牌字段以避免即时令牌过期,并且在实际上载时也有相同的问题(未经授权)。你最终解决了这个问题吗?如果是,怎么做?
{
  "access_token" : "1/8xbJqaOZXSUZbHLl5EOtu1pxz3fmmetKx9W8CV4t79M",
  "token_type" : "Bearer",
  "expires_in" : 3600
}
    try{
        // Client init 
        $client = new Google_Client();
        $client->setClientId($client_id);
        $client->setClientSecret($client_secret);
        $client->setApplicationName($applicationName);

        $client->setAccessToken($result);

        if ($client->getAccessToken()) {

            if($client->isAccessTokenExpired()) {
                // @TODO Log error 
                echo 'Access Token Expired!!<br/>'; // Debug
            }

            $youtube = new Google_YoutubeService($client);

            $videoPath = "./test.mp4";

            // Create a snipet with title, description, tags and category id
            $snippet = new Google_VideoSnippet();
            $snippet->setTitle("fmgonzalez test " . time());
            $snippet->setDescription("fmgonzalez test " . time() );
            $snippet->setTags(array("tag1", "tag2"));

            // Numeric video category. See
            // https://developers.google.com/youtube/v3/docs/videoCategories/list
            $snippet->setCategoryId("22");

            // Create a video status with privacy status. Options are "public", "private" and "unlisted".
            $status = new Google_VideoStatus();
            $status->privacyStatus = "public";

            // Create a YouTube video with snippet and status
            $video = new Google_Video();
            $video->setSnippet($snippet);
            $video->setStatus($status);

            // Size of each chunk of data in bytes. Setting it higher leads faster upload (less chunks,
            // for reliable connections). Setting it lower leads better recovery (fine-grained chunks)
            $chunkSizeBytes = 1 * 1024 * 1024;

            // Create a MediaFileUpload with resumable uploads
            $media = new Google_MediaFileUpload('video/*', null, true, $chunkSizeBytes);
            $media->setFileSize(filesize($videoPath));

            // Create a video insert request
            $insertResponse = $youtube->videos->insert("status,snippet", $video,
                array('mediaUpload' => $media));

            $uploadStatus = false;

            // Read file and upload chunk by chunk
            $handle = fopen($videoPath, "rb");
            $cont = 1;
            while (!$uploadStatus && !feof($handle)) {
                $chunk = fread($handle, $chunkSizeBytes);
                $uploadStatus = $media->nextChunk($insertResponse, $chunk);
                echo 'Chunk ' . $cont . ' uploaded <br/>';
                $cont++;
            }

            fclose($handle);

            echo '<br/>OK<br/>';

        }else{
            // @TODO Log error 
            echo 'Problems creating the client';
        }

    } catch(Google_ServiceException $e) {
        print "Caught Google service Exception ".$e->getCode(). " message is ".$e->getMessage(). " <br>";
        print "Stack trace is ".$e->getTraceAsString();
    }catch (Exception $e) {
        echo $e->getMessage();
    }
"error": {
    "errors": [
    {
        "domain": "youtube.header",
        "reason": "youtubeSignupRequired",
        "message": "Unauthorized",
        "locationType": "header",
        "location": "Authorization"
    }
    ],
    "code": 401,
    "message": "Unauthorized"
}