In your test, try changing this line:
$this->actingAs($user, 'api');
to:
$this->actingAs($user, 'passport');
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I am on the latest Laravel with initial passport installation
In my test i have -
$userEmail = '[email protected]';
$userPassword = 'my-password';
$userApiSecretName = 'token';
$userApiSecretRedirect = 'https://test.com/callback';
$user = factory(User::class)->create([
'email' => $userEmail,
'password' => $userPassword,
]);
$this->actingAs($user, 'api');
$client = factory(Client::class)->state('password_client')->create([
'user_id' => auth()->user()->id,
]);
$response = $this->post('/oauth/token', [
'form_params' => [ //with or without the 'form_params' part
'grant_type' => 'password',
'client_id' => $client->id,
'client_secret' => $client->secret,
'username' => $userEmail,
'password' => $userPassword,
],
]);
I receive The authorization grant type is not supported by the authorization server
But works fine when I use it from the axios such as
axios.post('/oauth/token', {
grant_type: 'password',
client_id: 'the client id',
client_secret: 'the secret',
username: '[email protected]',
password: 'my-password',
})
From the test it does not work but from the axios works fine.
Any help will be appreciated. Thanks
Actually, looking closer at the documentation, you want to use the Passport specific actingAs:
https://laravel.com/docs/7.x/passport#testing
Passport's actingAs method may be used to specify the currently authenticated user as well as its scopes. The first argument given to the actingAs method is the user instance and the second is an array of scopes that should be granted to the user's token:
use App\User;
use Laravel\Passport\Passport;
public function testServerCreation()
{
Passport::actingAs(
factory(User::class)->create(),
['create-servers']
);
$response = $this->post('/api/create-server');
$response->assertStatus(201);
}
Please or to participate in this conversation.