解决 OAuth 2.0 报错 unauthorized_client: The client is not authorized 修复指南
排查 OAuth 2.0 换取 Token 提示 unauthorized_client: The client is not authorized to use this grant type 错误,配置客户端授权模式白名单。
问题现象
应用程序调用 OAuth 2.0 Token 端点时,请求被拒绝:
{
"error": "unauthorized_client",
"error_description": "The client is not authorized to use this grant type: client_credentials"
}
或者使用 refresh_token 刷新令牌时提示没有权限。
触发根因
在 OAuth 2.0 / OIDC 规范中:
- 每个客户端注册时(Client ID)都被分配了明确的应用类型(Public Client 纯前端单页应用、Confidential Client 受信任服务端应用、Native 移动端);
- 授权服务器基于安全考虑,为每个客户端单独维护了一份允许的授权模式白名单(Allowed Grant Types);
- 如果在控制台未勾选开启
Client Credentials或Refresh Token,使用未被授权的模式请求时直接返回unauthorized_client。
解决步骤
1. 登录认证控制台开启对应授权模式
进入 Keycloak、Auth0、Okta 或自研 OAuth2 管理后台:
- 找到对应的 Client;
- 在 Capability config 或 Grant Types 中,勾选开启所需的模式:
Standard flow(authorization_code);Direct access grants(password,仅限遗留系统);Service accounts roles(client_credentials);Refresh token。
2. 核对请求体中 grant_type 参数拼写
确保 POST 请求体(Content-Type: application/x-www-form-urlencoded)中的参数命名完全一致:
curl -X POST https://auth.example.com/oauth/token -H "Content-Type: application/x-www-form-urlencoded" -d "grant_type=client_credentials" -d "client_id=my-service" -d "client_secret=SecretKey123"
验证确认
再次发送换取令牌请求,服务端返回 HTTP 200 OK 并正常下发 Access Token,unauthorized_client 错误彻底排除。


