草庐IT

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

coder 2024-04-15 原文

我正在尝试使用 API v3 将视频直接上传到 Youtube。

我正在使用服务帐户场景(https://developers.google.com/accounts/docs/OAuth2?hl=es&csw=1#scenarios),我解决了google-api-php-client库中的一些问题(读取p12文件并避免isAccessTokenExpired总是返回假)。

<?php
/** Config */ 
$private_key_password = 'notasecret';
$private_key_file = 'xxxxxxxx-privatekey.p12';
$applicationName = 'xxxxx-youtube';
$client_secret = 'CLIENT_SECRET';
$client_id = 'xxxxxxxxxxxxx.apps.googleusercontent.com';
$service_mail = 'xxxxxxxxxxx@developer.gserviceaccount.com';
$public_key = 'xxxxxxxxxxx';

/** Constants */ 
$scope = 'https://www.googleapis.com/auth/youtube';
$url_youtube_token = 'https://accounts.google.com/o/oauth2/token';

/** Create and sign JWT */ 
$jwt = new Google_AssertionCredentials($service_mail, $scope, $private_key_file, $private_key_password, $url_youtube_token);
$jwt_assertion = $jwt->generateAssertion();

/** Use JWT to request token */
$data = array(
    'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
    'assertion' => $jwt_assertion,
);

// use key 'http' even if you send the request to https://...
$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data),
    ),
);

$context = stream_context_create($options);
$result = file_get_contents($url_youtube_token, false, $context);

此时我在 json 响应中获得了访问 token ,如下所示:

{
  "access_token" : "1/8xbJqaOZXSUZbHLl5EOtu1pxz3fmmetKx9W8CV4t79M",
  "token_type" : "Bearer",
  "expires_in" : 3600
}

没有“created”、“refresh_token”和“id_token”字段。因此,我修复了 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();
    }

但我收到一条“无法开始可续传上传” 消息。

调试,Google_MediaFileUpload 中的方法 getResumeUri 我有这个响应正文:

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

我找到了有关其他场景的示例,但没有找到有关此场景的示例。

我应该怎么做才能最终上传视频文件?关于这种情况的任何例子?

提前致谢。

最佳答案

它可能看起来微不足道,但您是否在要将视频上传到的帐户中至少创建了一个 channel 。我对 access_token 使用了与您几乎相同的解决方法,然后遇到了同样的问题,直到我进入我的 Youtube 帐户上传部分并看到消息在上传视频之前至少创建了一个 channel 。希望对您有所帮助。

关于PHP Youtube API v3 - 直接上传 - 未经授权的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21723447/

有关PHP Youtube API v3 - 直接上传 - 未经授权的消息的更多相关文章

  1. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  2. ruby - 使用 Ruby 通过 Outlook 发送消息的最简单方法是什么? - 2

    我的工作要求我为某些测试自动生成电子邮件。我一直在四处寻找,但未能找到可以快速实现的合理解决方案。它需要在outlook而不是其他邮件服务器中,因为我们有一些奇怪的身份验证规则,我们需要保存草稿而不是仅仅发送邮件的选项。显然win32ole可以做到这一点,但我找不到任何相当简单的例子。 最佳答案 假设存储了Outlook凭据并且您设置为自动登录到Outlook,WIN32OLE可以很好地完成此操作:require'win32ole'outlook=WIN32OLE.new('Outlook.Application')message=

  3. Ruby - 如何将消息长度表示为 2 个二进制字节 - 2

    我正在使用Ruby,我正在与一个网络端点通信,该端点在发送消息本身之前需要格式化“header”。header中的第一个字段必须是消息长度,它被定义为网络字节顺序中的2二进制字节消息长度。比如我的消息长度是1024。如何将1024表示为二进制双字节? 最佳答案 Ruby(以及Perl和Python等)中字节整理的标准工具是pack和unpack。ruby的packisinArray.您的长度应该是两个字节长,并且按网络字节顺序排列,这听起来像是n格式说明符的工作:n|Integer|16-bitunsigned,network(bi

  4. ruby-on-rails - 在 Flash 警报 Rails 3 中显示错误消息 - 2

    如果我在模型中设置验证消息validates:name,:presence=>{:message=>'Thenamecantbeblank.'}我如何让该消息显示在闪光警报中,这是我迄今为止尝试过的方法defcreate@message=Message.new(params[:message])if@message.valid?ContactMailer.send_mail(@message).deliverredirect_to(root_path,:notice=>"Thanksforyourmessage,Iwillbeintouchsoon")elseflash[:error]

  5. ruby-on-rails - 在 RSpec 中,如何以任意顺序期望具有不同参数的多条消息? - 2

    RSpec似乎按顺序匹配方法接收的消息。我不确定如何使以下代码工作:allow(a).toreceive(:f)expect(a).toreceive(:f).with(2)a.f(1)a.f(2)a.f(3)我问的原因是a.f的一些调用是由我的代码的上层控制的,所以我不能对这些方法调用添加期望。 最佳答案 RSpecspy是测试这种情况的一种方式。要监视一个方法,用allowstub,除了方法名称之外没有任何约束,调用该方法,然后expect确切的方法调用。例如:allow(a).toreceive(:f)a.f(2)a.f(1)

  6. ruby-on-rails - 闪存消息存储在哪里? - 2

    我以为它们存储在cookie中-但不,检查cookie没有任何结果。session也不存储它们。那么,我在哪里可以找到它们?我需要这个来直接设置它们(而不是通过flashhash)。 最佳答案 它们存储在inyoursessionstore.自rails2.0以来的默认设置是cookie存储,但请检查config/initializers/session_store.rb以检查您是否使用默认设置以外的东西。 关于ruby-on-rails-闪存消息存储在哪里?,我们在StackOverf

  7. Ruby SSL 错误 - sslv3 警报意外消息 - 2

    我正在尝试在ruby​​脚本中连接到服务器https://www.xpiron.com/schedule。但是,当我尝试连接时:require'open-uri'doc=open('https://www.xpiron.com/schedule')我收到以下错误消息:OpenSSL::SSL::SSLError:SSL_connectreturned=1errno=0state=SSLv2/v3readserverhelloA:sslv3alertunexpectedmessagefrom/usr/local/lib/ruby/1.9.1/net/http.rb:678:in`conn

  8. ruby-on-rails - Ruby on Rails - 需要在每周的特定时间将消息发送到电子邮件 - 2

    我想知道我应该如何着手这个项目。我需要每周向人们发送一次电子邮件。但是,这必须在每周的特定时间自动生成并发送。编码有多难?我需要知道是否有任何书籍可以提供帮助,或者你们中的任何人是否可以指导我。它必须使用ruby​​onrails进行编程。因此有一个网络服务和数据库集成。干杯 最佳答案 为什么这么复杂?您只需安排工作。您可以使用Delayed::Job例如。Delayed::Job让您可以使用run_at符号在特定时间安排作业,如下所示:Delayed::Job.enqueue(SendEmailJob.new(...),:run_

  9. ruby-on-rails - 访问授权和访问 token 之间的区别 - 2

    Doorkeeper中Token和Grant的区别我搞不清楚。Doorkeeper在哪个时刻创建访问授权,何时创建访问token?文档似乎对此什么也没说,现在我正在阅读代码,但不是十几行。 最佳答案 我还建议阅读documentationofoauth2据我了解,Doorkeeper也是基于该文档中描述的协议(protocol)。在doorkeeper中,你会先获得accessgrant,然后是accesstoken。访问授权通常只存在很短的时间(doorkeeper中的默认值为10分钟)。您将通过向api-url/oauth/au

  10. ruby-on-rails - 使用 ruby​​ on rails 在 json 中发送错误消息 - 2

    我正在验证ruby​​onrails中的输入字段。我检查用户是否输入或填写了这些字段。如果假设name字段未填写,则向用户发送一条错误消息,指示name字段未填写。其他错误也是如此。我如何使用ruby​​onrails在json中发送这种消息。这是我现在正在做的。这个模型validates:email,:name,:company,:presence=>truevalidates_format_of:email,:with=>/\A[a-z0-9!#\$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#\$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9

随机推荐