Laravel Passport启用客户端凭证授权时遇unsupported_grant_type错误排查
排查Laravel Passport Client Credentials Grant的
unsupported_grant_type错误 这个unsupported_grant_type错误我之前也碰到过,大概率是客户端类型不匹配或者请求参数有误导致的,咱们一步步来排查:
1. 确认你创建的是Client Credentials类型的客户端
你执行的php artisan passport:client默认会生成用于Password Grant的客户端,而Client Credentials Grant需要专门的「机器对机器」客户端。你需要重新创建客户端:
php artisan passport:client --client
执行命令后,输入客户端名称就能直接生成适配Client Credentials Grant的client_id和client_secret。
2. 检查Postman请求的参数是否正确
确保你的POST请求满足以下要求:
- 请求URL:
http://你的域名/oauth/token - 请求方法:POST
- 以
form-data或x-www-form-urlencoded传递以下参数:grant_type: 必须准确填写为client_credentials(拼写不能错,少个s都会触发错误)client_id: 重新生成的客户端IDclient_secret: 重新生成的客户端密钥scope: 可以留空字符串(如果没定义权限范围),或者填写你需要的自定义权限(前提是你在AuthServiceProvider里提前定义过)
3. 确认Passport的配置是否到位
检查AuthServiceProvider
确保app/Providers/AuthServiceProvider.php的boot方法里正确注册了Passport路由:
public function boot() { $this->registerPolicies(); Passport::routes(); // 如果需要自定义权限范围,可以在这里添加示例: // Passport::tokensCan([ // 'view-data' => '查看数据权限', // 'edit-data' => '编辑数据权限', // ]); }
检查auth.php配置
确认config/auth.php里的API守卫驱动设置为passport:
'guards' => [ 'api' => [ 'driver' => 'passport', 'provider' => 'users', ], ],
4. 清除缓存确保配置生效
执行以下命令清除配置和路由缓存,避免旧配置干扰:
php artisan config:clear php artisan route:clear
完成以上步骤后,再用Postman重新请求,应该就能拿到正常的access_token了。如果还是有问题,可以查看Laravel的日志文件(storage/logs/laravel.log),里面会有更详细的错误细节帮你定位问题。
内容的提问来源于stack exchange,提问作者Matthias




