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

时间:2017-08-30 07:25:06

标签: php laravel-5 passport.js

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

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

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

当我使用邮递员去路线时

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:客户端错误: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

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

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

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

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

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

SoapUI

这是邮递员请求的内容。

HTTP(S) Test Script Recorder

这是我在哪里混淆。

没有参数通过此POST请求传递。 我的api启用了CORS,没有收到飞行前错误。 两个请求的标题都相同。 唯一的区别是,正在使用邮差和使用Axios。 除了通过邮递员或浏览器提出请求的地方外,没有任何变化。

1 个答案:

答案 0 :(得分:0)

浏览器中的

内容类型是text / html 而在邮递员中是application / json

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