954630688

489 经验值

这个错误是提示缺少参数,检查你的参数是否正确
可以对照这个看下

Class TokenProxy
{
    protected $http;

    public function __construct (Client $http)
    {
        $this->http = $http;
    }

    public function login ($email, $password)
    {
        if (auth()->attempt(['email' => $email, 'password' => $password])) {
            return $this->proxy([
                'username' => $email,
                'password' => $password
            ]);
        } else {
            return response()->json([
                'status'  => 'error',
                'message' => '请重新输入'
            ], 421);
        }
    }

    public function proxy (array $data = [])
    {
        $data = array_merge($data, [
            'grant_type'    => 'password',
            'client_id'     => client_id,
            'client_secret' => client_secret,
        ]);
        $response = $this->http->post('http://xxx/api/oauth/token', [
            'form_params' => $data
        ]);

        $token = json_decode((string)$response->getBody(), true);

        return response()->json([
            'token'      => $token['access_token'],
            'expires_in' => $token['expires_in']
        ])->cookie('refreshToken', $token['refresh_token'], 864000, null, null, false, true);

    }
}

检查出来了,client_id 后面多了一个空格,所以就一直导致那个错误

代码

$response = $this->http->request('post', 'http://ruicao.io/api/oauth/token', [
            'form_params' => [
                'username'      => '[email protected]',
                'password'      => '123456',
                'client_id '    => '1',
                'client_secret' => 'PXrLBReX7gPjheLcfXJzF223GnI1uDHv8xw3SsZB',
                'grant_type'    => 'password',
                'scope'         => '',
            ]
        ]);

报错信息

Client error: `POST http://ruicao.io/api/oauth/token` resulted in a `400 Bad Request` response:\n
{"error":"invalid_request","message":"The request is missing a required parameter, includes an invalid parameter value,  (truncated...)\n

提示缺少参数,已经将值都写在 form_params 里了