【问题标题】:Google OAuth2 Invalid token format error on redirecting to PWA重定向到 PWA 时出现 Google OAuth2 无效令牌格式错误
【发布时间】:2020-09-19 11:46:44
【问题描述】:

使用 Google OAuth 登录时出现错误。使用谷歌登录的桌面网站或移动浏览器使用没有问题。但是用户想在设备上安装的网站的移动浏览器和 PWA 上登录,然后在点击谷歌登录后移动浏览器重定向到 PWA。 PWA 从 GET 获取令牌,但给出“无效令牌格式”错误。

“未捕获的 InvalidArgumentException:/vendor/google/apiclient/src/Google/Client.php:449 中的令牌格式无效”

从谷歌返回的 URL 是:

https://example.com/google/?code=4/tokenxxx&scope=email个人资料https://www.googleapis.com/auth/userinfo.emailhttps://www.googleapis.com/auth/userinfo.profileopenid&authuser=1&hd=xx.com&prompt=none

$accessToken = $this->gClient->fetchAccessTokenWithAuthCode( trim( $_GET['code'] ) );
$this->gClient->setAccessToken( $accessToken ); // error line

桌面使用没有错误,也没有移动浏览器重定向到 PWA。

我需要一些想法。

编辑: $this->gClient->fetchAccessTokenWithAuthCode( trim( $_GET['code'] ) ) 函数返回“invalid_grant”错误。

【问题讨论】:

    标签: php authentication oauth progressive-web-apps manifest


    【解决方案1】:

    我在 PWA 上暂时解决了这个问题,再次重定向到登录 url。

    if ( isset( $accessToken['error'] ) ) {
        die( Url::redirect( HTTPSURL . "social/google/" ) );
    }
    

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题,并注意到在 Android 上的 PWA 身份验证的情况下,向 OAuth2 回调处理程序(用户在身份验证后由 Google 发送到的 URL)发出了两个相同的请求,时间差为一两秒钟。在内部,该处理程序向 Google 服务器请求 OAuth 令牌以换取授权代码。显然,该代码只能使用一次,因此 Google 服务器第二次以 400 响应。似乎为我的 App Engine 应用程序修复它的方法是将缓存添加到 OAuth2 回调处理程序(将“缓存控制”设置为例如“ public, max-age=600'),以便第二个请求获得缓存响应。

      【讨论】:

        猜你喜欢
        • 2017-03-28
        • 2023-03-04
        • 1970-01-01
        • 2017-02-25
        • 1970-01-01
        • 2014-08-22
        • 2021-09-11
        • 2018-10-14
        • 2013-05-02
        相关资源
        最近更新 更多