首页 > 解决方案 > invalid_grant 用于密码 grant_type Laravel Passport

问题描述

我正在尝试为我的 API 创建一个 access_token,但我遇到了显示此消息的问题:

"error": "invalid_client",
"error_description": "Client authentication failed",
"message": "Client authentication failed"

这是我的请求数据:

{
  "username":"neal.jacobi@example.org",
  "password":"test1234",
  "grant_type":"password",
  "client_id": "2",
  "client_secret":"jScq3DMMeZctypnYb7f1ClEHyzybwTK1Yisqo09E"
}

这是我的 oauth_clients 表:

oauth_clients_table

标签: laravellaravel-passport

解决方案


您需要在请求正文中发送以下内容。

{
  "username":"neal.jacobi@example.org",
  "password":"test1234",
  "grant_type":"password",
  "client_id": "2",
  "client_secret":"jScq3DMMeZctypnYb7f1ClEHyzybwTK1Yisqo09E"
}

正在通过以下方法从请求正文验证客户端凭据。

// League\OAuth2\Server\Grant\AbstractGrant 
// line 253 
protected function getClientCredentials(ServerRequestInterface $request)
    {
        [$basicAuthUser, $basicAuthPassword] = $this->getBasicAuthCredentials($request);

        $clientId = $this->getRequestParameter('client_id', $request, $basicAuthUser); // It is fetching from request body

        if (\is_null($clientId)) {
            throw OAuthServerException::invalidRequest('client_id');
        }

        $clientSecret = $this->getRequestParameter('client_secret', $request, $basicAuthPassword);

        if ($clientSecret !== null && !\is_string($clientSecret)) {
            throw OAuthServerException::invalidRequest('client_secret');
        }

        return [$clientId, $clientSecret];
    }

推荐阅读