API 调用在邮递员上返回,但在调用并登录浏览器时返回 500 内部服务器错误

Dan*_*ark 5 php passport.js laravel-5

这与 Laravel 5.4 及其 Passport 密码授予有关。

\n\n

我有获取 access_token 供用户使用的路线,效果完全正常。

\n\n

如果当前访问令牌过期,我还有一个刷新令牌的路线。

\n\n

当我使用 Postman 到达路线时

\n\n
http://192.168.10.10/refresh\n
Run Code Online (Sandbox Code Playgroud)\n\n

我得到这个有效的返回:

\n\n
"data": {\n    "access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOi...deleting most of it.",\n    "expires_in": 600\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

但是,当我通过浏览器点击路由时,通过以下代码使用 axios:

\n\n
let  headers = { \'Content-type\': \'application/json\' }\nreturn axios.post("http://192.168.10.10/refresh", {headers: headers}).then(res => {\n    if (res) return res;\n        }).catch(err => {\n            if (err) return err.response;\n        });\n
Run Code Online (Sandbox Code Playgroud)\n\n

我收到 HTTP 500 错误状态代码。

\n\n

我也在跟踪 laravel 日志中的错误,这是堆栈跟踪。

\n\n

[2017-08-30 07:21:41] local.ERROR:GuzzleHttp\\Exception\\ClientException:客户端错误:POST http://192.168.10.10/oauth/token导致400 Bad Request响应:\n{"error":"invalid_request","message":"请求缺少必需的参数,包括无效的参数值,(被截断...)\n 在 /home/vagrant/Code/work/vendorgraphs-api/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:113

\n\n

此错误的另一部分是它可能是格式错误的值。

\n\n

对我来说没有意义,我已经尝试了从直接从 PHP 代码发出curl 请求以及使用 http_query_builder 功能的所有方法。

\n\n
Cache-Control \xe2\x86\x92no-cache, private\nConnection \xe2\x86\x92keep-alive\nContent-Type \xe2\x86\x92application/json\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是 Postman 上通过请求设置的内容。我也从浏览器发送这些标头。关于可能导致该问题的原因有什么想法吗?这真让我抓狂。

\n\n
    public function refreshToken(Request $request) \n{     \n    if (!is_null($request)) {\n        $refreshToken = $request->cookie(self::REFRESH_TOKEN);\n\n        $http = new Guzzle();\n        $response = $http->request(\'POST\',\'http://192.168.10.10/oauth/token\', [\n            \'form_params\' => [\n                \'grant_type\' => \'refresh_token\',\n                \'refresh_token\' => $refreshToken,\n                \'client_id\' => env("PASSWORD_GRANT_CLIENT_ID"),\n                \'client_secret\' => env("PASSWORD_GRANT_SECRET"),\n                \'scope\' => \'\',\n            ]\n        ]);\n        $data = json_decode($response->getBody());\n        $this->cookie->queue(\n            self::REFRESH_TOKEN,\n            $data->refresh_token,\n            864000, // 10 days\n            null,\n            null,\n            false,\n            true // HttpOnly\n        );\n\n        return response()->json([\n            \'payload\' => [\n                \'access_token\' => $data->access_token,\n                \'expires_in\' => $data->expires_in\n            ]\n        ]);\n\n    } else {\n        return response()->json("Could not refresh token", 401);\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

请求的网络选项卡

\n\n

这是邮递员请求的内容。

\n\n

在此输入图像描述

\n\n

这就是我困惑的地方。

\n\n

没有参数通过此 POST 请求传递。\n我的 api 上启用了 CORS,未收到飞行前错误。\n两个请求的标头相同。\n唯一的区别是使用了 Postman 和使用了 Axios。\n什么都没有更改,但通过 Postman 或浏览器发出请求的位置除外。

\n

小智 3

浏览器中的内容类型是text/html,而邮递员中的内容类型是application/json

将内容类型设置为 application/json 它应该可以工作