【问题标题】:PHP Google Api Code Exchange Results In "invalid_grant"PHP Google Api 代码交换结果“invalid_grant”
【发布时间】:2013-09-27 00:50:01
【问题描述】:

我知道大约有 20 个这样的帖子,但我已经浏览了所有这些帖子,到目前为止没有任何答案有帮助(尽管帮助我排除了一些问题)。

我正在用 PHP 编写一堆与 Youtube 相关的 api 函数,一旦我有访问代码(并且刷新访问代码也没有问题),我就没有问题抓取数据,但是当我尝试交换授权代码时对于访问令牌,我收到“invalid_grant”响应。

我还需要离线访问,因为我打算在没有用户交互的情况下进行 api 调用,并且预授权帐户不是一种选择。

注意事项:

  • 我已取消对 OAuth 游乐场的所有测试的授权
  • 我已将我的 unix 系统上的“*ntpdate *”设置为 google
  • 刷新令牌有效
  • 如果我从代码中删除 urlencode 并请求 uri 字符串,我会收到“invalid request”错误。有趣的是刷新令牌功能不需要它。

代码

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" }

【问题讨论】:

  • 你能把实际的请求和响应粘贴到网上吗

标签: youtube-api google-api oauth-2.0 google-oauth google-api-client


【解决方案1】:

从未找到解决方案,但通过使用高度无证的 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());

【讨论】:

  • 实际上这些都是高度记录的。他们可以找到here。您也可以为其他服务找到他们here
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-08
  • 2018-02-09
  • 2012-12-27
  • 1970-01-01
  • 2023-03-13
相关资源
最近更新 更多