【发布时间】: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