I had a very similar issue too:
The difference between your codes and mine:
In the routes/api.php
, i used only auth:api
.
I didn't create PassportServiceProvider.php
in the app folder.
In the Kernel.php
mine is client
not client_credentials
.
I used client_credentials
as grant_type
in the POST request call.
In the result I always got 401.
Until I created a user using Password Grant Client:
php artisan passport:client --password
And changed client_credentials
to password
in the POST request call:
$http = new GuzzleHttp\Client;
$response = $http->post('http://your-app.com/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => 'client-id',
'client_secret' => 'client-secret',
'username' => 'taylor@laravel.com',
'password' => 'my-password',
'scope' => '',
],
]);
$access_token = json_decode((string) $response->getBody(), true)['access_token'];
Put the access token returned in the Bearer of the headers, and it works.
And also you can get the current user using $request->user();
If you are using client_credentials
as grant_type
, it's going through the client middleware, so in the middleware auth:api
needs to be removed.