Laravel项目中Passport v12.3出现unsupported_grant_type错误排查
Laravel Passport v12.3 密码授权类型不支持问题排查
在Laravel项目中使用Passport v12.3时,请求获取access_token时遭遇unsupported_grant_type错误,尝试多种方法无效,此前看到的Passport::routes()解决方案在Passport v8版本后已移除,以下是该错误的可能原因及排查方向:
请求参数
{ "grant_type": "password", "client_id": "client_id_here", "client_secret": "client_secret_here", "username": "user@example.com", "password": "user_password", "provider": "users", "scope": "*" }
错误返回
{ "error": "unsupported_grant_type", "error_description": "The authorization grant type is not supported by the authorization server.", "hint": "Check that all required parameters have been provided", "message": "The authorization grant type is not supported by the authorization server." }
错误原因排查
- 密码授权类型未启用:Passport v10及以上版本默认不再启用密码授权,需手动创建密码授权客户端。执行
php artisan passport:install --password命令生成专用的密码授权客户端,而非默认的客户端类型。 - 请求参数格式错误:Passport的令牌端点默认仅接受
application/x-www-form-urlencoded格式的请求参数,而非JSON格式。若以JSON发送请求,服务端无法正确识别grant_type参数,导致报错。 - 客户端类型不匹配:确认使用的
client_id属于密码授权类型客户端。检查数据库oauth_clients表中对应客户端的password_client字段值是否为1(1表示密码授权客户端,0则不是)。 - 路由配置缺失:尽管
Passport::routes()已被移除,仍需确保令牌端点路由正确配置。在routes/api.php中添加如下路由:use Laravel\Passport\Http\Controllers\AccessTokenController; Route::post('/oauth/token', [AccessTokenController::class, 'issueToken'])->middleware('throttle'); - 配置文件异常:检查
config/passport.php是否存在,且grant_types配置项中包含password。若配置文件缺失,执行php artisan vendor:publish --tag=passport-config生成配置文件并确认配置正确。
内容的提问来源于stack exchange,提问作者Leena Alrababah
相关产品推荐
相关产品推荐

