我正在尝试为护照创建一个持有者令牌,以便我可以在内部使用我的 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"
有办法解决这个问题吗?
这对我有用
$client=新客户端(['verify'=
$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' => '*',
]
]);