php基于浏览器上传视频到youtube channel apiv3

标签 php api youtube upload oauth2

我有一个可以在我的 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));

我收到了身份验证 token ,用于向 youtube 帐户制作身份验证 token ,并且可以通过表单上传视频
$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 channel 。用户很满意,因为没有额外的登录。

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

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

新的代码片段:

当用户想要上传视频时,我从 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';

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

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

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

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

最佳答案

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

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

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

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

我无法让 token 工作,但之前已经让服务脚本工作......也许我们可以互相帮助? http://fb.com/jmt193 :)

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

关于php基于浏览器上传视频到youtube channel apiv3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41253563/

相关文章:

PHP 标签后 JavaScript 停止工作

php - 如何将 SQL 时间记录转换为秒?

php - SQL 中的数学运算符不排除 "NA"值

javascript - 从 Swapi API 获取数据 https ://swapi. co/api/people/

android - 升级到最新版本的Android YouTube API

flash - Blogger Atom/RSS 提要中的 YouTube 视频

javascript - 使用 YouTube API 从带有 JSON 提要的视频中获取所有评论

php - JavaScript:如何创建 JSONP?

javascript - 如何获取Postman中的请求参数?

javascript - 为什么我的 jquery .get() 结果无法按我给出的顺序显示