Joseph Crawford
Joseph Crawford

Reputation: 1500

Laravel Passport - Grant Type Not Supported

I have installed Laravel Passport per the documentation and I have modified all areas of my code that is required.

I am working on setting up Password Grant Tokens so that users will be able to get an API token when logging in with their username and password for the site. I am hitting an issue though when it comes to the grant_type. For some reason Laravel is complaining about an invalid grant type.

{
  "error": "unsupported_grant_type",
  "message": "The authorization grant type is not supported by the authorization server.",
  "hint": "Check the `grant_type` parameter"
}

These are the fields that I am posting to /oauth/token

client_id = 4
client_secret = SMiYE7XqDNtXKQnmkYmFnXxfAaV83vRhnJ9zwCtZ
username = [email protected]
password = **************
grant_type = password
scope = *

I have run php artisan passport:install and I have also tried running php artisan passport:client --password

Both commands worked and both created the client and secrets, however, I cannot seem to get past the error about the grant_type.

Any suggestions on what I should look at to get this solved so that Password Grant Tokens will work for me?

Upvotes: 23

Views: 40807

Answers (7)

Yehia Salah
Yehia Salah

Reputation: 1

Starting from Laravel Passport v12, Laravel no longer enables the password grant by default for security reasons. Instead, you need to explicitly enable it in your application.

To enable the password grant, call the enablePasswordGrant method in the boot method of your App\Providers\AppServiceProvider class:

/**
 * Bootstrap any application services.
 */

public function boot(): void
{
    Passport::enablePasswordGrant();
}

Upvotes: 0

Erich
Erich

Reputation: 2616

Extending the accepted answer with an implementation in Laravel.

You can use this to immediately authenticate an API user who has just registered:

use Illuminate\Support\Facades\Http;

$user = User::create($request->all());

$token = (object) Http::asForm()->post(url('/oauth/token'), [
    'grant_type' => 'password',
    'client_id' => $client_id,
    'client_secret' => $client_secret,
    'username' => $user->email,
    'password' => $user->password,
    'scope' => '*',
])->json();

return response()
    ->json($user->toArray())
    ->cookie('token', $token, $token->expires_in / 60);

Upvotes: 0

Abdullahi Abdulkabir
Abdullahi Abdulkabir

Reputation: 183

Reading the Laravel documentation saved me a lot of stress. The oauth\token is used to retrieve token using the grant type specified, the route will return a JSON response containing access_token, refresh_token, and expires_in attributes. The expires_in attribute contains the number of seconds until the access token expires (ref) You are meant to

  1. Install passport
  2. Publish the service providers and migrations and migrate.
  3. Setup a route for login/register to create an account and login.
  4. In your User model, add HasApiTokens from use Laravel\Passport\HasApiTokens;
  5. In your response in your login method, add the token as part of the response. enter image description here
  6. Test your response on postman enter image description here

Upvotes: 1

Alican Ali
Alican Ali

Reputation: 181

For me the problem was that i wasnt using Request $request, i was using RegisterRequest $request which i had created.

Upvotes: 1

Muhammad Hashim
Muhammad Hashim

Reputation: 129

I'm using Postman and I have put all parameters in Params. Postman shows the following response

{
    "error": "unsupported_grant_type",
    "message": "The authorization grant type is not supported by the authorization server.",
    "hint": "Check the `grant_type` parameter"
}

Now I put all parameters in Body and then press the Send button, It's working well.

Upvotes: 12

MT_Shikomba
MT_Shikomba

Reputation: 187

Initial URL

https://restfulapi.test/oauth/authorize?client_id=3&redirect_url=http://restfulapi.test?response_type=code

Solution

https://restfulapi.test/oauth/authorize?client_id=3&redirect_url=http://restfulapi.test&response_type=code

I had to replace the question mark before response_type with the &

Upvotes: 0

Joseph Crawford
Joseph Crawford

Reputation: 1500

It appears that you must send the parameters as form data and not in the headers like I was doing... Rookie Mistake!

Upvotes: 50

Related Questions