【问题标题】:Wrong number of segments in token (OAuth Google Api)令牌中的段数错误(OAuth Google Api)
【发布时间】:2015-02-01 20:41:23
【问题描述】:

我的最终目标是通过 Google Gmail API 向自己发送电子邮件。

这是我的问题。

当我获取访问令牌时,会弹出一个错误

 Fatal error: Uncaught exception 'Google_Auth_Exception' with message 'Wrong number of segments in token: '

我在这里看到Cloud endpoints oauth2 error “这并不意味着您的令牌无效”,但我遇到了一个中断脚本的致命错误。

我的访问令牌如下所示4/MqiIIl5K4S3D4iiieHshQt5D4M79oo07SbhMn22oe2o.cswa8t9ZuDAfJvIeHux6iLYXpNQmlAI

如果我用这个令牌刷新页面,我会得到另一个错误,即

'Error fetching OAuth2 access token, message: 'invalid_grant: Invalid code.'

这是我的代码

<?php

include_once "templates/base.php";
echo pageHeader("Simple API Access");

require_once realpath(dirname(__FILE__) . '/../autoload.php');


$client = new Google_Client();


$client_id = '114600397795-j5sn0gvsdrup0s8dcmsj49iojp3m9biu.apps.googleusercontent.com';
$client_secret = 'm3Dzankql_rs1OGICsA3Hbtc';
$redirect_uri = 'http://alupa.com/gmail/examples/simple-query.php';

$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);

$client->addScope("https://www.googleapis.com/auth/gmail.readonly");
$client->addScope("https://mail.google.com/");

$apiKey = "AIzaSyCWXxrTshKsotxEYNZZCXxdVXhLeku55cw"; 
$client->setDeveloperKey($apiKey);


if (isset($_GET['code'])) {
  $client->authenticate($_GET['code']);
  $_SESSION['access_token'] = $client->getAccessToken();
  //$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  //header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}else{

$client->setApplicationName("Client_Gmail_Examples");

}


if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
  $client->setAccessToken($_SESSION['access_token']);
} else {
  $authUrl = $client->createAuthUrl();
}

if ($client->getAccessToken()) {
  $_SESSION['access_token'] = $client->getAccessToken();
  $token_data = $client->verifyIdToken()->getAttributes();
}


?>
<div class="box">
  <div class="request">
<?php
if (isset($authUrl)) {
  echo "<a class='login' href='" . $authUrl . "'>Connect Me!</a>";
} else {
  echo "<a class='logout' href='?logout'>Logout</a>";
}
?>
  </div>
</div>

alupa.com 是我的本地域,我没有发现任何问题

我正在使用来自谷歌https://github.com/google/google-api-php-client的原始库

【问题讨论】:

  • 我也有同样的问题。令牌没有所有数据。你解决了吗?

标签: php oauth-2.0 google-api google-oauth oauth-php


【解决方案1】:

变化:

$token_data = $client->verifyIdToken()->getAttributes();

到:

$tuan = $client->getAccessToken();
$token_data = $client->verifyIdToken($tuan->id_token);

【讨论】:

    【解决方案2】:

    您需要添加范围 openid,然后您会获得一个 id_token 以及访问令牌。然后,您将 id 令牌与 $client->verifyIdToken

    一起使用

    【讨论】:

      猜你喜欢
      • 2018-10-28
      • 2013-07-21
      • 1970-01-01
      • 1970-01-01
      • 2012-11-06
      • 2018-09-08
      • 2013-07-31
      • 2017-06-20
      • 1970-01-01
      相关资源
      最近更新 更多