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

时间:2016-12-21 00:56:57

标签: 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));

我收到了auth令牌,用于为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;

我不需要任何上传视频的权限,用户只能使用表格中的标题,desc等,从计算机中获取视频并点击上传。一切都很好,用户可以将视频上传到我的YouTube频道。 useres ere满意,因为没有额外的登录。

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

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

新的代码段:

当用户想要上传视频时,我会检查来自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帐户存储视频,不需要有关用户谷歌帐户的信息。它非常烦人,因为用户必须登录到应用程序,登录后告诉你,必须登录到谷歌帐户......

是否有任何解决方法/解决方案可以将上传视频转换到我的YouTube频道,与之前没有oauth2一样容易吗?

1 个答案:

答案 0 :(得分:0)

您需要设置服务帐户,这意味着分配域范围的授权:https://console.developers.google.com/apis/credentials

&#34;要支持服务器到服务器的交互,请先在API控制台中为项目创建一个服务帐户。如果要访问G Suite域中用户的用户数据,请将域范围内的访问权限委派给服务帐户。 然后,您的应用程序准备使用服务帐户的凭据进行授权的API调用,以从OAuth 2.0 auth服务器请求访问令牌。 最后,您的应用程序可以使用访问令牌来调用Google API。&#34;

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

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

我无法让令牌上班,但之前已经开始使用服务脚本......也许我们可以互相帮助? http://fb.com/jmt193:)

如果你问他们,我会回答问题。