DEBUG.WIKI
阿里云;从建站到云上业务;云服务器 · 数据库 · 对象存储;查看产品 →
PanstarCloud:部署应用,接入 AI,云服务器与 AI 网关
IPPeak:从目标地区访问网页,住宅代理、SEO 监测、广告验证

解决 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 规范中:

  1. 每个客户端注册时(Client ID)都被分配了明确的应用类型(Public Client 纯前端单页应用、Confidential Client 受信任服务端应用、Native 移动端);
  2. 授权服务器基于安全考虑,为每个客户端单独维护了一份允许的授权模式白名单(Allowed Grant Types);
  3. 如果在控制台未勾选开启 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 错误彻底排除。