检索客户端凭据授予令牌返回401未经授权

时间:2019-04-17 05:27:47

标签: laravel oauth-2.0 laravel-passport

我正在设置Laravel Passport的“客户授予”,并使用Guzzle检索令牌,但是却出现401未经授权的错误。

我使用php artisan passport:client --client创建了客户端。 生成的客户端是:

Client ID: 1
Client secret: NmJsEVFClXVqWJuQnZTWA3bnZEVZ0KaC13anHZt1

我将它们保存在.env文件中并运行命令

php artisan config:clear

这是我的代码:

        $url = 'http://hub.local/oauth/token';

        $client_secret = env('CLIENT_SECRET');
        $client_id = env('CLIENT_ID');
        $client = new Client();

        try {
            $response = $client->post($url, [
                'json' => [
                    'grant_type' => 'client_credentials',
                    'client_id' => $client_id,
                    'client_secret' => $client_secret,
                ],
                'headers' => [
                    'Content-Type' => 'application/json',
                ],
            ]);
            return $response;
        } catch (RequestException $e) {
            dd($e);
        } catch (Exception $e) {
            dd($e);
        }

以及由此产生的错误消息:

ClientException {#660 ▼
  -request: Request {#649 ▶}
  -response: Response {#657 ▶}
  -handlerContext: []
  #message: """
    Client error: `POST http://hub.local/oauth/token` resulted in a `401 Unauthorized` response:
    {"error":"invalid_client","error_description":"Client authentication failed","message":"Client authentication failed"}
    """
  #code: 401
  #file: "C:\repos\client\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php"
  #line: 113

当我在Insomnia中使用相同的参数和值尝试请求时,该请求有效。 似乎我还不能发布图片。 https://i.imgur.com/w6Ollin.jpg https://imgur.com/fXpzKjj.jpg

我正在使用Laravel 5.8.12,Guzzle 6.3.3和Passport 7.2.2。 我想念什么?

3 个答案:

答案 0 :(得分:0)

更新:仅当我在同一本地xampp服务器上运行(Laravel)客户端和服务器实例时,才出现问题。我将它们分开以在各自的xampp实例上运行,并且身份验证没有问题。当仅在本地进行测试时,令我感到困惑的是该请求适用于Insomnia,但不适用于浏览器。我假设此客户端授予的“机器对机器”部分是看到我的客户端+服务器共享相同的xampp实例作为无法识别的内容。无论如何,没有任何实际问题。希望这可以帮助其他人。

答案 1 :(得分:0)

在laravel doc中有一个这样的示例:

Route::get('/callback', function (Request $request) {
    $http = new GuzzleHttp\Client;

    $response = $http->post('http://your-app.com/oauth/token', [
        'form_params' => [
            'grant_type' => 'authorization_code',
            'client_id' => 'client-id',
            'client_secret' => 'client-secret',
            'redirect_uri' => 'http://example.com/callback',
            'code' => $request->code,
        ],
    ]);

    return json_decode((string) $response->getBody(), true);
});

当我用我的信息测试这个时,我变成了这样的东西:

Route::get('/users/login', function (Request $request) {
    $http = new GuzzleHttp\Client;

    $response = $http->post('http://payment.devenv/oauth/token', [
        'form_params' => [
            'grant_type' => 'password',
            'client_id' => 2,
            'client_secret' => 'z8YWlJeaW0d7o7SFyPLISUxYOcO5CxjGrZ5YuItl',
            'username' => 'athamidn@gmail.com',
            'password' => '123456',
            'scope' => '',
            'code' => 200,
            'redirect_uri' => 'http://example.com/callback',
        ],
    ]);
    return json_decode((string) $response->getBody(), true);
});

我收到与您的错误相同的错误。几个小时后,我遇到了问题:

在数据库oauth_clients表中,这是我的信息:

2 name: Laravel Password Grant Client redirect: http://localhost

当我将重定向URL更改为此时,它可以工作。 最后:

Route::get('/users/login', function (Request $request) {
    $http = new GuzzleHttp\Client;

    $response = $http->post('http://payment.devenv/oauth/token', [
        'form_params' => [
            'grant_type' => 'password',
            'client_id' => 2,
            'client_secret' => 'z8YWlJeaW0d7o7SFyPLISUxYOcO5CxjGrZ5YuItl',
            'username' => 'athamidn@gmail.com',
            'password' => '123456',
            'scope' => '',
            'code' => 200,
            'redirect_uri' => 'http://localhost'
        ],
    ]);
    return json_decode((string) $response->getBody(), true);
});

答案 2 :(得分:0)

我相信您可以通过执行 /storage 重新创建存储在 php artisan passport:install 文件夹中的护照公钥和私钥。然后您可以在 .env 文件中定义这些键并使用 client_idclient_secret 传递它。

command output
Personal access client created successfully.
Client ID: 31
Client secret: xxxx
Password grant client created successfully.
Client ID: 32
Client secret: xxxx

/.env
PERSONAL_CLIENT_ID=31
PERSONAL_CLIENT_SECRET=xxxx
PASSWORD_CLIENT_ID=32
PASSWORD_CLIENT_SECRET=xxxx

POST - API parameters - /oauth/token
'client_id' => env('PASSWORD_CLIENT_ID'),
'client_secret' => env('PASSWORD_CLIENT_SECRET')

当这些私钥和公钥与已创建的私钥不正确匹配时,可能会发生此错误。

干杯!

相关问题