php - Google+ 身份验证 : refreshToken() returns invalid_grant error

标签 php google-oauth google-api-php-client

我正在使用 Laravel 构建一个 Web 应用程序,该应用程序使用 Google 帐户作为登录方法。我已经从 Github 下载了 Google+ PHP API,并编写了一个单例类来访问 Google+ 服务。此类的片段如下所示:

// Returns a client ready to receive an access token.
private static function getClient() {
  if (!self::$client) {
    self::$client = new Google_Client();
    self::$client->setApplicationName(self::$application_name);
    self::$client->setClientId(self::$client_id);
    self::$client->setClientSecret(self::$client_secret);
    self::$client->setAccessType('offline');
    // I'm only requesting the plus.login and userinfo.profile scopes.
    self::$client->setScopes(self::$scopes);
    // RedirectURI = 'postmessage'
    self::$client->setRedirectUri(self::$redirect_uri);
  }
  return self::$client;
}

// Returns a client ready to be used for API calls.
private static function getClient2() {
  $c = self::getClient();
  // getToken returns the token session variable. This is the full
  // JSON encoded token we got after authenticating the code.
  $token = self::getToken();
  $c->setAccessToken($token);
  if ($c->isAccessTokenExpired()) {
    // getRefreshToken returns the refresh_token session variable.
    // This is a JSON decoded string that only contains
    // the refresh_token value.
    $refreshToken = self::getRefreshToken();

    // This is the line that blows up.
    $c->refreshToken($refreshToken);

    // This code has never been reached :(
    $newToken = $c->getAccessToken();
    self::storeToken($newToken);
  }

  // If the access_token hasn't expired, we can merrily begin using
  // the client to construct a Google_Service_Plus object.
  return $c;
}

我正在实现混合客户端/服务器流程。用户首次登录时,将调用 JavaScript 回调,发送要进行身份验证的代码。用户已通过身份验证,refresh_token 存储在数据库和当前 session 中,我可以在 access_token 的生命周期内成功进行 API 调用。

一小时后,访问 token 过期。我在 getClient2() 中对此进行了测试,并尝试使用从 session 中检索到的 refresh_token 刷新 access_token(我已经打印了 session 变量和可以确认它们存在并且是 json_decoded)。然后我得到了漂亮的描述性错误

Error refreshing the OAuth2 token, message: 'invalid_grant'

此时所有依赖于 Google+ 的页面都显示错误页面。除非用户返回到网站主页,否则 JavaScript 会再次开始登录流程,从而允许服务器在另一个小时内获得有效的 access_token,否则该网站将变得无响应。

如果需要,我可以发布更多单例类,但考虑到我能够在第一个小时内对用户进行身份验证并发出请求,我认为这就是所有相关内容。

类似但没有帮助的问题:

最佳答案

如果您希望使用刷新 token 刷新访问 token ,您可以这样做

$client->refreshToken( $refresh_token );
$_SESSION['token'] = $client->getAccessToken();

如果您尝试刷新您最近授予的刷新 token 以外的内容,您将获得返回的 invalid_grant。

关于php - Google+ 身份验证 : refreshToken() returns invalid_grant error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25300126/

相关文章:

php - Gmail API : Get list of messages labelled with a specific label in php

php - 照亮\数据库\QueryException : SQLSTATE[HY000]: General error: 1813 Tablespace for table

express - Express 和 Passport 的 Google token 身份验证不起作用

php - 如何获取数组中的第一个非零值和最后一个非零值索引?

node.js - 在 Heroku 中部署时,Google 回调 url 返回 400

android - 如果没有重定向 uri,Android 版 Google 登录如何工作?

php - Google Analytics API - 认证后无法添加服务

php - 客户端无权使用此方法检索访问 token ,或者客户端无权访问任何请求的范围

php - 使用 Microsoft SQL 查询反序列化 PHP

php - 如何在没有整个插件的情况下调用WordPress插件功能