具有公共访问权限的Web应用程序的Google OAuth 2.0刷新令牌

驼鹿

我收到以下错误:

OAuth 2.0访问令牌已过期,并且刷新令牌不可用。对于自动批准的响应,不返回刷新令牌

我有一个仅允许我的服务器访问的Web应用程序,初始身份验证工作正常,但是一个小时后,上述错误消息弹出。我的脚本如下所示:

require_once 'Google/Client.php';     
require_once 'Google/Service/Analytics.php';       
session_start();

$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$client->setDeveloperKey("{MY_API_KEY}");
$client->setClientId('{MY_CLIENT_ID}.apps.googleusercontent.com');
$client->setClientSecret('{MY_KEY}');
$client->setRedirectUri('{MY_REDIRECT_URI}');
$client->setScopes(array('https://www.googleapis.com/auth/gmail.readonly'));

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

if (!$client->getAccessToken() && !isset($_SESSION['token'])) {
    $authUrl = $client->createAuthUrl();
    print "<a class='login' href='$authUrl'>Connect Me!</a>";
}

如何为此设置刷新令牌?

编辑1:我也尝试过使用服务帐户执行此操作。我遵循了GitHub上可用的文档:

https://github.com/google/google-api-php-client/blob/master/examples/service-account.php

我的脚本如下所示:

session_start();
include_once "templates/base.php";
require_once 'Google/Client.php';
require_once 'Google/Service/Gmail.php';
$client_id = '{MY_CLIENT_ID}.apps.googleusercontent.com'; 
$service_account_name = '{MY_EMAIL_ADDRESS}@developer.gserviceaccount.com ';
$key_file_location = 'Google/{MY_KEY_FILE}.p12';
echo pageHeader("Service Account Access");
    if ($client_id == ''
|| !strlen($service_account_name)
|| !strlen($key_file_location)) {
  echo missingServiceAccountDetailsWarning();
}
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$service = new Google_Service_Gmail($client);
if (isset($_SESSION['service_token'])) {
  $client->setAccessToken($_SESSION['service_token']);
}
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
    $service_account_name,
    array('https://www.googleapis.com/auth/gmail.readonly'),
    $key
);
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
  $client->getAuth()->refreshTokenWithAssertion($cred);
}

这将返回以下消息:

刷新OAuth2令牌时出错,消息:'{“错误”:“ invalid_grant”}''

跟踪此代码的源代码后,可以在 Google/Auth/OAuth2.php

有问题的方法refreshTokenRequest

private function refreshTokenRequest($params)
{
    $http = new Google_Http_Request(
    self::OAUTH2_TOKEN_URI,
    'POST',
    array(),
    $params
    );
    $http->disableGzip();
    $request = $this->client->getIo()->makeRequest($http);

    $code = $request->getResponseHttpCode();
    $body = $request->getResponseBody();
    if (200 == $code) {
      $token = json_decode($body, true);
      if ($token == null) {
        throw new Google_Auth_Exception("Could not json decode the access token");
      }

      if (! isset($token['access_token']) || ! isset($token['expires_in']))     {
        throw new Google_Auth_Exception("Invalid token format");
      }

      if (isset($token['id_token'])) {
        $this->token['id_token'] = $token['id_token'];
      }
      $this->token['access_token'] = $token['access_token'];
      $this->token['expires_in'] = $token['expires_in'];
      $this->token['created'] = time();
    } else {
      throw new Google_Auth_Exception("Error refreshing the OAuth2 token, message: '$body'", $code);
    }
  }

这意味着该$code变量为NULL。我在SO上找到了这篇文章:

刷新OAuth2令牌时发生错误{“错误”:“ invalid_grant”}

可以看到仍然没有首选的解决方案。这让我发疯。关于这一点的文档很少甚至没有,如果有人有解决方案,我敢肯定我不是唯一寻求它的人。

哈菲兹·迪万达里(Hafez Divandari)

每个access_token会在几秒钟后过期,并且需要使用refresh_token进行刷新,您正在寻找“离线访问”。您可以在此处遵循文档:

https://developers.google.com/accounts/docs/OAuth2WebServer#offline

要获得refresh_token,您只需运行一次此代码:

require_once 'Google/Client.php';

$client = new Google_Client();
$client->setClientId('{MY_CLIENT_ID}.apps.googleusercontent.com');
$client->setClientSecret('{MY_KEY}');
$client->setRedirectUri('{MY_REDIRECT_URI}');
//next two line added to obtain refresh_token
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
$client->setScopes(array('https://www.googleapis.com/auth/gmail.readonly'));

if (isset($_GET['code'])) {
    $credentials = $client->authenticate($_GET['code']);  

    /*TODO: Store $credentials somewhere secure */

} else {
    $authUrl = $client->createAuthUrl();
    print "<a class='login' href='$authUrl'>Connect Me!</a>";
}

$credentials包括访问令牌和刷新令牌。您必须将其存储以随时获取新的访问令牌。因此,当您想调用api时:

require_once 'Google/Client.php';
require_once 'Google/Service/Gmail.php';

/*TODO: get stored $credentials */

$client = new Google_Client();
$client->setClientId('{MY_CLIENT_ID}.apps.googleusercontent.com');
$client->setRedirectUri('{MY_REDIRECT_URI}');
$client->setClientSecret('{MY_KEY}');
$client->setScopes(array('https://www.googleapis.com/auth/gmail.readonly'));
$client->setAccessToken($credentials);

$service = new Google_Service_Gmail($client);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

具有单页面应用程序刷新访问令牌的Oauth2隐式流

来自分类Dev

Google OAuth2 API刷新令牌

来自分类Dev

如何为asp.net Web应用程序获取Google Api OAuth 2刷新令牌?

来自分类Dev

带有刷新令牌的春季Google OAuth2

来自分类Dev

无法使用Google OAuth 2获取刷新令牌

来自分类Dev

OAuth 2 Google API刷新令牌为空

来自分类Dev

无法从Google API OAuth2刷新令牌

来自分类Dev

无法使用Google OAuth 2获取刷新令牌

来自分类Dev

在OAuth2中刷新令牌有什么好处

来自分类Dev

Spring OAuth2没有提供刷新令牌

来自分类Dev

在ASPMVC应用程序中使用OAuth2刷新令牌

来自分类Dev

在React Redux应用程序中使用OAuth2刷新令牌

来自分类Dev

在Rails应用程序中使用Omniauth-oauth2刷新令牌

来自分类Dev

Google Oauth 2获取访问令牌并从授权码刷新令牌

来自分类Dev

Google Oauth 2获取访问令牌并从授权码刷新令牌

来自分类Dev

Google Oauth2:刷新OAuth2令牌时出错,消息:'{“ error”:“ invalid_grant”}'

来自分类Dev

Google OAuth2:以编程方式吊销特定的刷新令牌(但无法访问应用)

来自分类Dev

Google OAuth 2.0刷新令牌

来自分类Dev

OAuth访问令牌和刷新令牌创建

来自分类Dev

OAuth 2.0访问令牌和刷新令牌

来自分类Dev

OAuth访问令牌和刷新令牌创建

来自分类Dev

获取新的访问令牌时,不应替换oauth2中的刷新令牌

来自分类Dev

使用phpleague / oauth2-server使用刷新令牌创建新的新访问令牌

来自分类Dev

如何保留OAuth 2刷新令牌

来自分类Dev

如何保留OAuth 2刷新令牌

来自分类Dev

Cookie中的OAuth2刷新令牌

来自分类Dev

OAuth 2-如何使用刷新令牌

来自分类Dev

Facebook是否具有OAuth的刷新令牌?

来自分类Dev

OAuth2访问令牌

Related 相关文章

热门标签

归档