gpt4 book ai didi

php - API 调用在 postman 上返回,但在浏览器中调用和登录时返回 500 内部服务器错误

转载 作者:搜寻专家 更新时间:2023-10-31 21:50:06 27 4
gpt4 key购买 nike

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

我有获取 access_token 供用户使用的路由,它工作得很好。

如果当前访问 token 过期,我还有一个刷新 token 的路径。

当我使用Postman打路由的时候

http://192.168.10.10/refresh

我得到了这个有效的:

"data": {
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOi...deleting most of it.",
"expires_in": 600
}

但是,当我通过此代码使用 axios 从浏览器访问路由时:

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

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

我也在跟踪 laravel 日志以查找错误,这是堆栈跟踪。

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

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

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

Cache-Control →no-cache, private
Connection →keep-alive
Content-Type →application/json

这是通过请求在 Postman 上设置的内容。我也从浏览器发送这些 header 。关于可能导致问题的任何想法?这让我发疯。

    public function refreshToken(Request $request) 
{
if (!is_null($request)) {
$refreshToken = $request->cookie(self::REFRESH_TOKEN);

$http = new Guzzle();
$response = $http->request('POST','http://192.168.10.10/oauth/token', [
'form_params' => [
'grant_type' => 'refresh_token',
'refresh_token' => $refreshToken,
'client_id' => env("PASSWORD_GRANT_CLIENT_ID"),
'client_secret' => env("PASSWORD_GRANT_SECRET"),
'scope' => '',
]
]);
$data = json_decode($response->getBody());
$this->cookie->queue(
self::REFRESH_TOKEN,
$data->refresh_token,
864000, // 10 days
null,
null,
false,
true // HttpOnly
);

return response()->json([
'payload' => [
'access_token' => $data->access_token,
'expires_in' => $data->expires_in
]
]);

} else {
return response()->json("Could not refresh token", 401);
}
}

The network's tab for the request

这是 postman 请求的内容。

enter image description here

这就是我感到困惑的地方。

此 POST 请求未传递任何参数。CORS 在我的 api 上启用,没有收到飞行前错误。两个请求的 header 相同。唯一的区别是,使用的是 Postman 而使用的是 Axios。除了通过 Postman 或浏览器发出请求的位置外,没有任何变化。

最佳答案

浏览器中的内容类型是 text/html而在 postman 中是 application/json

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

关于php - API 调用在 postman 上返回,但在浏览器中调用和登录时返回 500 内部服务器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45954398/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com