1

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

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

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

笔记:

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

代码

访问.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" }
4

1 回答 1

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());
于 2013-09-22T20:44:01.997 回答