OAuth 2.0访问令牌已过期,并且刷新令牌不可用 [英] OAuth 2.0 access token has expired, and a refresh token is not available

查看:334
本文介绍了OAuth 2.0访问令牌已过期,并且刷新令牌不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于Web的应用程序,它使用Google OAuth2.0作为登录框架。之前一直有效,直到昨天。访问令牌过期后,应用无法获取刷新令牌。除此之外,请求权限页面已更改为具有离线访问权限,而不是了解您在Google上的身份和查看您的电子邮件

I have a web based application which use Google OAuth2.0 as the login framework. It works nicely previously until yesterday. The applcation couldn't get the refresh token after the access token expired. Besides that, the "Request for permission" page had change to "Have offline access" instead of "Know who you are on Google" and "View you email"

,请求权限页面将请求访问知道您在Google上的身份和查看您的电子邮件。用户注销并尝试第二次登录后,请求权限页面也将相同。

Originally, the "Request for permission" page will request the access to "Know who you are on Google" and "View you email". After user logout and attempts second login, the "Request for permission" page will be the same too.

但是,直到昨天,请求权限页面更改为具有脱机访问权限。访问令牌过期后,我收到以下错误消息:

However, until yesterday, the "Request for permission" page changed to "Have offline access". After the access token is expired, I got the error messsage below:

PHP致命错误:未捕获的异常 Google_AuthException,消息为 OAuth 2.0访问令牌已过期,并且刷新令牌不可用。 /home2/xxxx/public_html/test/google-api-php-client/src/auth/Google_OAuth2.php:221

PHP Fatal error: Uncaught exception 'Google_AuthException' with message 'The OAuth 2.0 access token has expired, and a refresh token is not available. Refresh tokens are not returned for responses that were auto-approved.' in /home2/xxxx/public_html/test/google-api-php-client/src/auth/Google_OAuth2.php:221

我尝试了 $ client-> setAccessType('online'); 。但是,我仍然遇到这个致命错误。以下是获取访问令牌的代码:

I tried $client->setAccessType('online'); . However, I still got this fatal error with me. Below is my code to get the access token :

    if ($client->getAccessToken()) {
      $token = $client->getAccessToken();
      $authObj = json_decode($token);
      $refreshToken = $authObj->refresh_token;
      $user = $oauth2->userinfo->get();
      $me = $plus->people->get('me');
      $email = filter_var($user['email'], FILTER_SANITIZE_EMAIL); // get the USER EMAIL ADDRESS using OAuth2

      $optParams = array('maxResults' => 100);
      $activities = $plus->activities->listActivities('me', 'public', $optParams);


      $_SESSION['access_token'] = $client->getAccessToken();
    } else {
      $authUrl = $client->createAuthUrl();
    }

我尝试搜索类似的问题,但找不到。从昨天开始就发生了。在此之前,我从未对代码进行任何更改。

I tried to search for similar problem like me but I couldn't find one. This happened since yesterday. Before this, I never made any change on the codes.

推荐答案

Fabian Parzefall用他的评论帮助我解决了这个问题。

With his comments, Fabian Parzefall helped me getting this fixed.

这是我的脚本:

if($client->isAccessTokenExpired()) {

    $authUrl = $client->createAuthUrl();
    header('Location: ' . filter_var($authUrl, FILTER_SANITIZE_URL));

}

实际上非常简单。我没有直接要求他单击连接我按钮(由GA API团队提供的演示脚本进行设置),而是直接将其重定向。
不确定这是否是正确/安全的方法,但是那对我来说是个可行的方法!

It's actually pretty simple. Instead of asking him to click the "connect me" button (as put by the demo script provided by the GA API team), I redirect him directly. Not sure if it's the proper/safer way, but that's the one working for me right now!

这篇关于OAuth 2.0访问令牌已过期,并且刷新令牌不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆