youtube-api - 在"invalid_grant" 中,PHP Google代码交换结果

  显示原文与译文双语对照的内容
122 5

我认识到这些文章有 20个,但是我已经查过了所有的,没有回答到目前为止。

我们在PHP中写了大量的Youtube相关函数,并且一旦获得了访问代码的访问码,我就没有问题,但是当我尝试交换访问令牌的授权代码时,我收到了响应。

我还需要脱机访问,因为我打算使api调用无需用户交互,并且预先授权帐户。

注释:

  • 我已经授权了每个测试和OAuth游乐场
  • 我已经在unix系统上设置了"* ntpdate *"
  • 刷新令牌工作
  • 如果从代码和请求uri字符串中删除英镑,我将收到一个错误。 有趣的是,它不需要刷新令牌函数。

代码

access.php


$ytclient = new ytClient;

if(isset($_GET['code'])){
 $ytclient->auth_code = $_GET['code'];
 $ytclient->exchangeToken();
}else{
 $ytclient->authorize();
}

ytClient


function authorize(){
 include('config.php');
 echo"<script>window.location = 'https://accounts.google.com/o/oauth2/auth?redirect_uri=".urlencode( $config->domain.'/lib/access.php')."&response_type=code&client_id=**************&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyt-analytics-monetary.readonly+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyt-analytics.readonly+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube.readonly+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube.upload+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutubepartner&approval_prompt=force&access_type=offline'</script>";
}
function exchangeToken(){
 $postArr["client_id"] = ($config->ytclientid);
 $postArr["client_secret"] = ($config->ytclientsecret);
 $postArr["code"] = urlencode($this->auth_code);
 $postArr["request_uri"] = urlencode($config->domain."/lib/access.php");
 $postArr["grant_type"] ="authorization_code";
 $accessObj = $this->get_yt_json("https://accounts.google.com/o/oauth2/token", $postArr, 1);
}

function get_yt_json($url, $postArr, $mode){
 if($mode == 1){
 $curl = curl_init($url);
 curl_setopt($curl, CURLOPT_POST, true);
 curl_setopt($curl, CURLOPT_POSTFIELDS, $postArr);
 }elseif($mode == 2){
 $url. ="?".http_build_query($postArr);
 $curl = curl_init($url);
 }
 curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
 curl_setopt($curl, CURLOPT_HEADER, true);
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

 $rawJson = curl_exec($curl);
 curl_close($curl);
 echo $rawJson;
/*
 $obj = json_decode($rawJson);

 return ($obj);
 */
}

响应


HTTP/1.1 400 Bad Request
Cache-Control: no-cache, no-store, max-age=0, must-revalidate 
Pragma: no-cache 
Expires: Fri, 01 Jan 1990 00:00:00 GMT 
Date: Sat, 21 Sep 2013 18:58:31 GMT 
Content-Type: application/json 
X-Content-Type-Options: nosniff 
X-Frame-Options: SAMEORIGIN 
X-XSS-Protection: 1; mode=block 
Server: GSE 
Alternate-Protocol: 443:quic 
Transfer-Encoding: chunked 

{"error" :"invalid_grant" }

时间: 原作者:

71 2

没有找到解决方案,但使用高度文档化的google php api => https://code.google.com/p/google-api-php-client/ 解决了它。

就我所知,快速简单的解决方案--和魔术。 下面是使用api对某人进行终身令牌认证的代码,可能会帮助某人:


include(dirname(__FILE__).'/google-api-php-client/src/Google_Client.php');

$client = new Google_Client();
$client->setApprovalPrompt('force');
$client->setAccessType('offline');
$client->setScopes('https://www.googleapis.com/auth/yt-analytics-monetary.readonly https://www.googleapis.com/auth/yt-analytics.readonly https://www.googleapis.com/auth/youtube https://www.googleapis.com/auth/youtube.readonly https://www.googleapis.com/auth/youtube.upload https://www.googleapis.com/auth/youtubepartner');

/* Auth the client and get Token Object */
$auth = $client->authenticate();
$token = json_decode($client->getAccessToken());

原作者:
...