1

我在谷歌身份验证上遇到问题,它工作了一个月,但几天后我收到了这个错误:

Fatal error: Uncaught exception 'apiAuthException' with message 
'Invalid token format' in /home/project/html/google/auth/apiOAuth2.php:127 Stack trace:
#0 /home/project/html/google/auth/apiOAuth2.php(89): apiOAuth2->setAccessToken('{? "access_tok...')
#1 /home/project/html/google/apiClient.php(132): apiOAuth2->authenticate(Array)
#2 /home/project/html/hk/connects/google.php(22): apiClient->authenticate()
#3 {main} thrown in /home/project/html/google/auth/apiOAuth2.php on line 127

在 apiOAuth2.php 我有代码:

$accessToken = json_decode($accessToken, true);
if (! isset($accessToken['access_token']) || ! isset($accessToken['expires_in']) || ! isset($accessToken['refresh_token'])) {
  throw new apiAuthException("Invalid token format");
}

我注意到谷歌没有向我发送 $accessToken['refresh_token']。它似乎不是来自谷歌,因为我在http://stackoverflow.com上做了正确的连接

也许这是我的代码的原因:

session_start();

$client = new apiClient();
$plus = new apiPlusService($client);

if (!isset($_GET['code'])) {
  header('Location: '.$client->createAuthUrl()); // Calls the same page
} else {
  $client->authenticate(); // Fails at this level
}

编辑:

我想出了一种方法,就像我不知道 refresh_token 是什么一样,因为我添加了这一行:

if (!isset($accessToken['refresh_token'])) $accessToken['refresh_token'] = 12345678910;

它暂时有效...

4

1 回答 1

2

google 身份验证 api 需要一个名为“access_type”的新显式参数来获取有效的刷新令牌。使用此参数,您声明您需要对帐户进行离线访问,并且 api 会为您提供刷新令牌。

下载自动处理新的必需参数的最新谷歌 PHP SDK

于 2011-12-12T11:49:51.417 回答