内部生成laravel护照令牌。 401错误未授权

时间:2018-12-11 14:32:50

标签: laravel laravel-passport

我正在尝试为护照创建不记名令牌,以便可以在内部使用我的api路由。我将客户机ID和客户机密钥发送到/ oauth / token的发布请求。生成不记名令牌的代码如下所示。

public function generateToken() {
    try {
        $req = Request::create('/oauth/token', 'POST', [
            'grant_type' => 'client_credentials',
            'client_id' => '1',
            'client_secret' => 'POe81tKvmZ0DhBdtwdM7VPoV5tTKobVYzSc9A0RX',
            'scope' => '*',
        ]);
        $res = app()->handle($req);
        return $res;
    } catch (Exception $e){
        log::critical('Failed to generate bearer token.');
    } catch (error $e) {
        log::critical('Failed to generate bearer token.');
    }

}

但是,当尝试执行此请求时,我获得了401授权。

  #content: "{"error":"invalid_client","message":"Client authentication failed"}" #version: "1.1"#statusCode: 401 #statusText: "Unauthorized"

反正有解决办法吗?

1 个答案:

答案 0 :(得分:0)

这对我有用

$client = new client(['verify' => false]);

$response = $client->post($url, [
            'form_params' => [
                'client_id' => 5,
                // The secret generated when you ran: php artisan passport:install
                'client_secret' => 'HxLB5LVi3HhHxYkoHVZinoYUz5Ab3HnbWmiB1KBd',
                'grant_type' => 'client_credentials',
                'scope' => '*',
            ]
        ]);