Search code examples
laravelpostmanlaravel-9laravel-passport

Laravel passport unsupported_grant_type with client credentials


I'm trying to do machine-to-machine authentication for an api I'm building (no user involved). I've followed the limited documentation for Laravel Passport surrounding the client credentials option, but I keep getting unsupported_grant_type.

My Api route:

Route::post('/oauth/token', [AccessTokenController::class, 'issueToken'])->middleware(['throttle']);

I can see through ngrok that the request is going through, so I feel like it's configured correctly. I've also added a test endpoint which works fine.

Route::get('/test', fn () => response()->json(['message' => 'Welcome to the API!']));

My config in auth.config:

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'passport',
        'provider' => 'users',
        'hash' => false,
    ],
],

int config/passport.php I set the guard to api:

'guard' => 'api',

I've got Content-Type set to application/x-www-form-urlencoded and Accept headers set to application/json.

Any idea what am I doing wrong?

postman request and response

UPDATE:

For anyone who stumbles across this, apparently I was missing the Content-Length header, which I didn't realize was required, but is. I just happened to try it and it solved my problem. Hope it helps someone.


Solution

  • Since I ran into this problem AGAIN, and stumbled upon my previous question which helped me solve it, I figured I'd go ahead and post the solution to the problem so when I google it again next time I run into this problem it'll be here.

    The Content-Length header is required. If you don't have it, you'll get an Unsupported Grant Type error message, which will leave you stumped and googling.

    You're welcome, future self.