Получите токен доступа при отправке с FormRequest - PullRequest
0 голосов
/ 23 января 2020

Я создаю логин, используя экземпляр formRequest в качестве параметра, после того как я проверил доступ пользователя, я добавляю к запросу параметры, которым нужен oauth-сервер.

однако я получаю ошибка сервера oauth:

{
    "error": "unsupported_grant_type",
    "error_description": "The authorization grant type is not supported by the authorization server.",
    "hint": "Check that all required parameters have been provided",
    "message": "The authorization grant type is not supported by the authorization server."
}

, но когда я изменяю экземпляр моего параметра на Request, я больше не получаю эту ошибку.

Шаги для воспроизведения:

class AuthController extends Controller
{
    use ThrottlesLogins;

    public function store(LoginRequest $loginRequest)
    {
        //$loginRequest->validated();
        if ($this->hasTooManyLoginAttempts($loginRequest)) {
            $this->fireLockoutEvent($loginRequest);
            return $this->sendLockoutResponse($loginRequest);
        }
        if (Auth::attempt($this->credentials($loginRequest))){
            $client = $this->getClient($loginRequest->name);
            $params = [
                'grant_type'    => 'password',
                'client_id'     => $client->id,
                'client_secret' => $client->secret,
                'username'      => $loginRequest->email,
                'password'      => $loginRequest->password,
                'scopes'         => 'fd',
            ];
            $loginRequest->request->add($params);
            $req = Request::create('oauth/token', 'POST');
            $response = Route::dispatch($req)->getContent();
            return $response;
        }

        $this->incrementLoginAttempts($loginRequest);
        $this->sendFailedLoginResponse($loginRequest);
    }
}

1 Ответ

0 голосов
/ 24 января 2020

Попробуйте добавить приложение Content-Type / x- www-form-urlencoded в качестве заголовка к вашему запросу к 'oauth / token'

$req = Request::create('oauth/token', 'POST');
$req->headers->set('Content-Type', 'application/x-www-form-urlencoded');
$response = Route::dispatch($req)->getContent();
...