You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel Passport客户端凭证模式出现unsupported_grant_type错误求助

Laravel Passport 机器对机器认证报错 unsupported_grant_type 排查与解决

我正在为开发的API实现无用户参与的机器对机器认证,已按照Laravel Passport客户端凭证选项的文档配置,但始终返回unsupported_grant_type错误。

路由配置

API令牌请求路由:

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

测试端点(已通过ngrok确认请求正常可达):

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

认证配置

config/auth.php中的守卫设置:

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

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

config/passport.php中已指定使用api守卫:

'guard' => 'api',

请求头已设置Content-Type: application/x-www-form-urlencoded和Accept: application/json,但问题依旧。


问题解决(更新)

经过排查,发现问题出在缺少Content-Length请求头,添加该请求头后,认证请求正常通过,问题解决。希望能帮到遇到相同问题的开发者。

内容的提问来源于stack exchange,提问作者hyphen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 13:12:08