【问题标题】:php Browser-based Uploading video to youtube channel apiv3php 基于浏览器上传视频到 youtube 频道 apiv3
【发布时间】:2017-05-06 07:59:39
【问题描述】:

我的 youtube 帐户中有一个可以上传视频的应用程序,但升级到 YouTube API v3 后,情况就复杂了。

它使用了这个实现https://developers.google.com/youtube/2.0/developers_guide_protocol_browser_based_uploading

Youtube 授权以前是这样的。

$postData = "Email=".urlencode(Config::$youtubeEmail)."&Passwd=".urlencode(Config::$youtubePassword)."&service=youtube&source=".urlencode(Config::$youtubeAppName);
    $curl = curl_init("https://www.google.com/youtube/accounts/ClientLogin");
    curl_setopt($curl,CURLOPT_HEADER,"Content-Type:application/x-www-form-urlencoded");
    curl_setopt($curl,CURLOPT_POST,1);
    curl_setopt($curl,CURLOPT_POSTFIELDS,$postData);
    curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,0);
    curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,1);
    $response = curl_exec($curl);
    curl_close($curl);
    //print_r($response);exit();
    list($this->auth,$this->youtubeUser) = explode("\n",$response);
    list($this->authLabel,$this->authValue) = array_map("trim",explode("=",$this->auth));
    list($this->youtubeUserlabel,$this->youtubeUserValue) = array_map("trim",explode("=",$this->youtubeUser));

我收到了身份验证令牌,用于制作身份验证令牌到 youtube 帐户,并且可以通过表单上传视频

$youtubeVideoKeywords = ""; // This is the uploading video keywords.
    $youtubeVideoCategory = $this->getVideoCategory(); // This is the uploading video category. There are only certain categories that are accepted. See below the method
    $data = '<?xml version="1.0"?'.'>
    <entry xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:yt="http://gdata.youtube.com/schemas/2007">
        <media:group>
            <media:title type="plain">'.stripslashes($youtubeVideoTitle).'</media:title>
            <media:description type="plain">'.stripslashes($youtubeVideDescription).'</media:description>
            <media:category scheme="http://gdata.youtube.com/schemas/2007/categories.cat">'.$youtubeVideoCategory.'</media:category>
            <media:keywords>'.$youtubeVideoKeywords.'</media:keywords>
        </media:group>
        <yt:accessControl action="list" permission="denied"/>
    </entry>';
    $headers = array(
        'Authorization: Bearer ' . $this->authValue,
        'GData-Version: 2',
        'X-GData-Key: key=' . Config::$youtubeDevKey,
        'Content-Type: application/atom+xml; charset=UTF-8'
    );

    $curl = curl_init("http://gdata.youtube.com/action/GetUploadToken");
    curl_setopt($curl,CURLOPT_USERAGENT,$_SERVER["HTTP_USER_AGENT"]);
    curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($curl,CURLOPT_TIMEOUT,10);
    curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($curl,CURLOPT_POST,1);
    curl_setopt($curl,CURLOPT_FOLLOWLOCATION,1);
    curl_setopt($curl,CURLOPT_HTTPHEADER,$headers);
    curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
    curl_setopt($curl,CURLOPT_REFERER,true);
    curl_setopt($curl,CURLOPT_HEADER,0);
    $response = simplexml_load_string(curl_exec($curl));
    curl_close($curl);
    return $response;

我不需要任何权限来上传视频,用户只使用写标题、描述等的表格,从计算机上提取视频并点击上传。一切都很好,用户可以将视频上传到我的 youtube 频道。用户很满意,因为没有额外的登录。

但是现在,当我向https://www.google.com/youtube/accounts/ClientLogin 发出 curl 请求时,它说我需要 oauth2.0 身份验证服务。

所以我为我的服务生成了 oauth2 机密,对代码进行了更改,但是当用户想要使用这个应用程序时,他需要登录到谷歌帐户并允许使用网络应用程序的权限。

新代码sn-p:

当用户想要上传视频时,我会从 oauth2service 中检查身份验证代码,如果不存在,则生成 curl 代码

$url = 'https://accounts.google.com/o/oauth2/v2/auth?';
$url .= 'client_id='   . Config::$googleClientId;
$url .= '&redirect_uri=' . urlencode(Config::$googleRedirectUrl);
$url .= '&scope=' . urlencode("https://www.googleapis.com/auth/youtube");
$url .= '&response_type=code';

它让我返回重定向页面,当我获得代码时,将其存储到应用程序以供以后使用。用户写下有关视频的信息,然后点击下一步按钮。我创建了新线程来上传服务,使用生成的代码获取访问令牌,用于形成上传

$curl = curl_init('https://accounts.google.com/o/oauth2/token');

    $post_fields = array(
        'code'     => $code,
        'client_id'   => Config::$googleClientId,
        'client_secret' => Config::$googleSecretCode,
        'redirect_uri' => Config::$googleRedirectUrl,
        'grant_type'  => 'authorization_code'
    );
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post_fields));

    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/x-www-form-urlencoded'
    ));

    //send request
    $response = curl_exec($curl);
    curl_close($curl);

    $json = json_decode($response,true);

    $this->authValue = $json["access_token"];

    // The method returns response xml
    $response = false;
    $youtubeVideoKeywords = ""; // This is the uploading video keywords.
    $youtubeVideoCategory = $this->getVideoCategory(); // This is the uploading video category. There are only certain categories that are accepted. See below the method
    $data = '<?xml version="1.0"?'.'>
    <entry xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:yt="http://gdata.youtube.com/schemas/2007">
        <media:group>
            <media:title type="plain">'.stripslashes($youtubeVideoTitle).'</media:title>
            <media:description type="plain">'.stripslashes($youtubeVideDescription).'</media:description>
            <media:category scheme="http://gdata.youtube.com/schemas/2007/categories.cat">'.$youtubeVideoCategory.'</media:category>
            <media:keywords>'.$youtubeVideoKeywords.'</media:keywords>
        </media:group>
        <yt:accessControl action="list" permission="denied"/>
    </entry>';
    $headers = array(
        'Authorization: Bearer ' . $this->authValue,
        'GData-Version: 2',
        'X-GData-Key: key=' . Config::$youtubeDevKey,
        'Content-Type: application/atom+xml; charset=UTF-8'
    );

    $curl = curl_init("http://gdata.youtube.com/action/GetUploadToken");
    curl_setopt($curl,CURLOPT_USERAGENT,$_SERVER["HTTP_USER_AGENT"]);
    curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($curl,CURLOPT_TIMEOUT,10);
    curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($curl,CURLOPT_POST,1);
    curl_setopt($curl,CURLOPT_FOLLOWLOCATION,1);
    curl_setopt($curl,CURLOPT_HTTPHEADER,$headers);
    curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
    curl_setopt($curl,CURLOPT_REFERER,true);
    curl_setopt($curl,CURLOPT_HEADER,0);
    $response = simplexml_load_string(curl_exec($curl));
    curl_close($curl);
    return $response;

一切都很好,现在我已经有了表单,其中的操作是带有令牌的 youtube 服务器。一切正常。

但是现在需要用户同意才能使用 Web 应用程序,为什么?我有存储视频的 youtube 帐户,不需要有关用户 google 帐户的信息。这很烦人,因为用户必须登录到应用程序,登录后它告诉你,必须登录到谷歌帐户......

请问有没有什么变通方法/解决方案可以让我在没有 oauth2 的情况下像以前一样轻松地将视频上传到我的 youtube 频道?

【问题讨论】:

    标签: php api youtube upload oauth2


    【解决方案1】:

    您需要使用服务帐户进行设置,这意味着分配域范围的委派:https://console.developers.google.com/apis/credentials

    “要支持服务器到服务器的交互,请首先在 API 控制台中为您的项目创建一个服务帐户。如果您想访问 G Suite 域中用户的用户数据,请委派域范围的访问权限到服务帐户。 然后,您的应用程序准备通过使用服务帐户的凭据从 OAuth 2.0 身份验证服务器请求访问令牌来进行授权的 API 调用。 最后,您的应用程序可以使用访问令牌调用 Google API。”

    帮助:https://developers.google.com/identity/protocols/OAuth2ServiceAccount

    如果没有服务帐户,它将始终要求人类用户接受同意屏幕,而服务帐户会绕过同意屏幕,使其适用于您希望它在后台运行的此类操作。他们的文档中没有很好地涵盖它(或者我还没有找到好的资源......即使我觉得我已经把这个星球上的每一块石头都翻了)。

    我在让令牌工作时遇到问题,但之前让服务脚本工作过......也许我们可以互相帮助? http://fb.com/jmt193 :)

    如果您提出问题,我会回复。

    【讨论】:

      猜你喜欢
      • 2013-05-29
      • 2012-04-10
      • 2013-07-06
      • 2013-06-28
      • 2013-02-10
      • 2015-01-10
      • 1970-01-01
      • 2013-11-10
      • 2013-02-03
      相关资源
      最近更新 更多