【问题标题】:Get the token access at dispatch with FormRequest使用 FormRequest 在调度时获取令牌访问权限
【发布时间】:2020-05-10 04:14:34
【问题描述】:

我使用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);
    }
}

【问题讨论】:

  • 抱歉我不能回答你的问题,但是你知道你可以create为这样的用户提供一个令牌:$token = $user->createToken('My Token', ['scope-1'])->accessToken;吗?
  • 是的,我知道,但是我们可以制作一个刷新令牌吗? (用这种方法)@DelenaMalan

标签: php laravel authentication laravel-passport


【解决方案1】:

尝试将 Content-Type application/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();

【讨论】:

  • 它不起作用。我通过使用 HasApiTokens trait 的 createToken 方法来生成访问令牌并能够继续,从而劫持了这个问题。
猜你喜欢
  • 2012-01-19
  • 1970-01-01
  • 2013-06-23
  • 1970-01-01
  • 1970-01-01
  • 2019-11-05
  • 2012-06-12
  • 2017-11-05
  • 2014-05-08
相关资源
最近更新 更多