【问题标题】:Google Plus OAuth PHP 401 (Unauthorized)Google Plus OAuth PHP 401(未经授权)
【发布时间】:2016-08-05 16:58:15
【问题描述】:

我正在尝试使用their tutorial 使用 google plus 设置身份验证。我按照指示逐字逐句更改了signin.php 中的client idclient secret。作为记录,谷歌开发者控制台中启用了 google plus API。我也按照指示更新文件权限(chmod +x signin.phpchmod -R 555 vendor/)。但是,在加载我的身份验证 URL(恰好位于我的域的 auth_test/ 子目录中,然后单击登录按钮时,控制台会为 get 发送的 get 请求引发 401 (unauthorized) 我研究过问题并看到这可能是由无效令牌引起的,但我不明白这是怎么回事,因为所有内容都已在 singin.php. 中设置好,不胜感激...

【问题讨论】:

  • 您确定可以发出curl 请求吗?....也许您没有启用 curl 扩展。

标签: php oauth google-oauth google-plus-signin


【解决方案1】:

如果断开连接,您需要重置应用的状态以刷新$tocken

关于处理 API 错误的 Google API office 文档

401: Invalid Credentials

授权标头无效。您使用的访问令牌是 过期或无效。

{   "error": {
>     "errors": [
>       {
>         "domain": "global",
>         "reason": "authError",
>         "message": "Invalid Credentials",
>         "locationType": "header",
>         "location": "Authorization",
>       }
>     ],
>     "code": 401,
>     "message": "Invalid Credentials"   } }

建议的操作:使用长寿命的刷新访问令牌 刷新令牌。如果失败,引导用户通过 OAuth 流程, 如授权您的应用中所述

在 singin.php 的第 1 行也清楚地注释了它。 98:

    // Normally the state would be a one-time use token, however in our
    // simple case, we want a user to be able to connect and disconnect
    // without reloading the page.  Thus, for demonstration, we don't
    // implement this best practice.
    //$app['session']->set('state', '');

因此,在您的情况下,您的应用似乎已断开连接,从而导致 $token 变为空。因此在第 91 行强制执行此代码块

if (empty($token)) {
        // Ensure that this is no request forgery going on, and that the user
        // sending us this connect request is the user that was supposed to.
        if ($request->get('state') != ($app['session']->get('state'))) {
            return new Response('Invalid state parameter', 401);
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-08
    • 2017-09-27
    • 2012-12-20
    • 2012-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-09
    相关资源
    最近更新 更多