简体   繁体   English

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

[英]API call returns on postman, but returns a 500 internal server error when called and logged in browser

This is related to Laravel 5.4 and its Passport Password Grant.这与 Laravel 5.4 及其 Passport Password Grant 有关。

I have routes for obtaining an access_token for the user to use that works completely fine.我有获取 access_token 供用户使用的方法,该方法完全正常。

I also have a route to refresh the token should the current access token expire.如果当前访问令牌过期,我还有一个刷新令牌的方法。

When I use Postman to hit the route当我使用 Postman 打路线时

http://192.168.10.10/refresh

I get this back which is valid:我得到了这个有效的:

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

However when I hit the route from the browser, using axios via this code:但是,当我从浏览器点击路线时,通过以下代码使用 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;
        });

I get an HTTP 500 Error status code.我收到一个 HTTP 500 错误状态代码。

I'm tailing the laravel log for errors as well and this is the stack trace.我也在跟踪 laravel 日志中的错误,这是堆栈跟踪。

[2017-08-30 07:21:41] local.ERROR: GuzzleHttp\\Exception\\ClientException: Client error: POST http://192.168.10.10/oauth/token resulted in a 400 Bad Request response: {"error":"invalid_request","message":"The request is missing a required parameter, includes an invalid parameter value, (truncated...) in /home/vagrant/Code/work/vendorgraphs-api/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:113 [2017-08-30 07:21:41] local.ERROR: GuzzleHttp\\Exception\\ClientException: Client error: POST http://192.168.10.10/oauth/token导致400 Bad Request响应:{"error": "invalid_request","message":"请求缺少一个必需的参数,在/home/vagrant/Code/work/vendorgraphs-api/vendor/guzzlehttp/guzzle/src 中包含一个无效的参数值,(被截断...) /Exception/RequestException.php:113

The other part of this error is that it may be a malformed value.此错误的另一部分是它可能是格式错误的值。

Doesn't make sense to me and I've tried everything from making curl requests directly from PHP code and also used http_query_builder functionality.对我来说没有意义,我已经尝试了从直接从 PHP 代码发出 curl 请求的所有方法,还使用了 http_query_builder 功能。

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

This is what's set on Postman with the request.这就是 Postman 设置的请求。 I am sending those headers from the browser as well.我也从浏览器发送这些标头。 Any ideas on what might be causing the issue?关于可能导致问题的任何想法? This is driving me crazy.这让我发疯。

    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);
    }
}

请求的网络选项卡

And here's the content for the postman request.这是邮递员请求的内容。

在此处输入图片说明

HERE'S WHERE I'M CONFUSED.这就是我感到困惑的地方。

No parameters are being passed through this POST request.没有参数通过此 POST 请求传递。 CORS is enabled on my api, not receiving pre-flight errors. CORS 在我的 api 上启用,没有收到飞行前错误。 Headers are the same on both requests.两个请求的标头相同。 The only difference is, Postman being used and Axios being used.唯一的区别是使用 Postman 和使用 Axios。 Nothing changes, except for where the request is being made, via Postman or browser.除了通过邮递员或浏览器发出请求的位置外,没有任何变化。

content-type in your browser is text/html whereas in the postman is application/json浏览器中的内容类型是 text/html 而邮递员中的内容类型是 application/json

Set the content-type to application/json and it should work将内容类型设置为 application/json,它应该可以工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM