为了更安全的网络访问,请务必使用TLS1.2协议及规定内的加密套件。若使用协议是TLS1.0、TLS1.1或规定外的加密套件,可能无法正常访问华为账号服务。
关于TLS1.2协议及规定内加密套件的信息,可以点击这里进行详细了解:TLS协议及加密套件。
承载协议: HTTPS POST
接口方向: 开发者服务器->华为账号服务器
接口URL: https://oauth-login.cloud.huawei.com/oauth2/v3/token
数据格式:
请求消息:Content-Type: application/x-www-form-urlencoded
响应消息:Content-Type: application/json;charset=utf-8
| 参数 | 是否必选 | 参数类型 | 描述 |
|---|---|---|---|
| Content-Type | 是 | String | 取值为:application/x-www-form-urlencoded。 说明: Request Body传参,请务必遵循此请求头格式,否则可能导致请求失败,具体传参方式可参考示例代码。 |
| 参数 | 是否必选 | 参数类型 | 描述 |
|---|---|---|---|
| grant_type | 是 | String | 授权模式,本场景固定传“refresh_token”。 |
| client_id | 是 | String | 在创建应用后,由HarmonyOS 开发者中心为应用分配的唯一标识。参数取值详见查看应用信息中的OAuth 2.0客户端ID(凭据)-Client ID参数。 说明: 该参数与获取refresh_token参数时的Client ID必须一致,否则会报错(sub_error=20154),如出现此报错,请参考配置Client ID排查处理。 |
| client_secret | 是 | String | 在创建应用后,由HarmonyOS 开发者中心为应用分配的密钥(Client Secret)。参数取值详见查看应用信息中的OAuth 2.0客户端ID(凭据)-Client Secret参数。 |
| refresh_token | 是 | String | 通过获取用户级凭证获取的Refresh Token,用于刷新Access Token。 |
| scope | 否 | String | 该参数用于指定获取Access Token中的scope范围,需要是Refresh Token中包含的scope的子集,多个scope以空格分隔,当前最大支持同时传入150个scope。 - 如果没有传scope参数,则生成的Access Token包含的scope和Refresh Token中的scope相同。 - 如果传了scope参数,则Access Token所包含的scope是Refresh Token中的scope和入参scope取交集。 |
| supportAlg | 否 | String | 生成ID Token的算法,当前支持的算法如下: - PS256(推荐使用) - RS256 如果未指定该参数或指定的算法不在支持的范围内,则默认使用RS256。 |
POST /oauth2/v3/token HTTP/1.1 Host: oauth-login.cloud.huawei.com Content-Type: application/x-www-form-urlencoded grant_type=refresh_token&client_id=<client_id>&client_secret=<client_secret>&refresh_token=<refresh_token>
调用成功时,响应消息体返回如下:
| 参数 | 是否必选 | 参数类型 | 描述 |
|---|---|---|---|
| token_type | 是 | String | 固定字符串“Bearer”。 |
| access_token | 是 | String | Access Token,访问被权限管控资源的凭证。Access Token长度详见Access Token和Refresh Token长度限制要求。 |
| scope | 否 | String | Access Token中的scope,当Access Token不包含任何scope时,该字段不返回。详见入参scope字段说明。 |
| expires_in | 是 | Integer | Access Token的过期时间,单位:s。默认有效期为3600s。 |
| id_token | 否 | String | 当响应参数的scope中包含openid时,则会返回此参数(JWT格式)。ID Token的描述信息请参见验证ID Token有效性中ID Token描述。 |
调用失败时,响应消息体返回如下:
HTTP/1.1 200 OK
Content-Type: application/json;charset=utf-8
{
"access_token": "DgEAAN7qd*****U0TvQ/eXpE4x+gvhoYh5/UuzL",
"id_token": "eyJraW*****ifQ.eyJhdF9oYX*****Q2fQ.TT05lFYe*****vDwb_Gj1ccR59yyB2Ig",
"expires_in": 3600,
"scope": "openid profile email",
"token_type": "Bearer"
} HTTP/1.1 400 Bad Request
Content-Type: application/json
{
"sub_error": 12304,
"error_description": "invalid client_secret",
"error": 1203
} Java示例代码如下,运行前需要进行示例代码环境配置(请将此示例代码与工具类CallUtils放于同一路径下,如不在同一路径,请手动添加import)
import com.alibaba.fastjson2.JSONObject;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* 刷新用户级凭证
*/
public class RefreshTokenAPIDemo {
public static void main(String[] args) throws IOException {
// 刷新用户级凭证的接口URL
String url = "https://oauth-login.cloud.huawei.com/oauth2/v3/token";
// 授权模式,固定传"refresh_token"
String grantType = "refresh_token";
// 替换为实际的Client ID
String clientId = "<Client ID>";
// 替换为Client ID对应的Client Secret
String clientSecret = "<Client Secret>";
// 替换为Client ID获取的Refresh Token
String refreshToken = "<Refresh Token>";
JSONObject result = refreshToken(url, refreshToken, clientSecret, clientId, grantType);
// 解析获取scope
String scope = result.getString("scope");
// 解析获取token_type
String tokenType = result.getString("token_type");
// 解析获取access_token
String accessToken = result.getString("access_token");
// 解析获取expires_in
Integer expiresIn = result.getInteger("expires_in");
// 解析获取id_token
String idToken = result.getString("id_token");
}
private static JSONObject refreshToken(String url, String refreshToken, String clientSecret,
String clientId, String grantType) throws IOException {
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> request = new ArrayList<>();
request.add(new BasicNameValuePair("client_secret", clientSecret));
request.add(new BasicNameValuePair("refresh_token", refreshToken));
request.add(new BasicNameValuePair("client_id", clientId));
request.add(new BasicNameValuePair("grant_type", grantType));
request.add(new BasicNameValuePair("supportAlg", "PS256"));
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
httpPost.setEntity(new UrlEncodedFormEntity(request));
// 如需要自定义异常处理请使用api CallUtils#remoteCall(HttpUriRequest, BiFunction<CloseableHttpResponse,String,E>)
return CallUtils.toJsonObject(CallUtils.remoteCallOAuth(httpPost));
}
} | HTTP响应码 | 描述 | 解决方法 |
|---|---|---|
| 200 | 成功。 | - |
| 400 | 参数错误。 | 请根据业务响应主错误码以及业务响应子错误码进一步排查问题。 |
| 403 | 无权限访问。 | 通常是调用方网络安全策略阻止了访问,请检查网络环境配置。若仍无法解决,请通过在线提单提交问题。 |
| 404 | 找不到服务。 | 请检查请求URI是否正确。 |
| 405 | 不支持的http请求method。 | 请检查http请求method是否与接口说明一致。 |
| 500 | 服务内部错误。 | 请通过在线提单提交问题。 |
| 502 | 请求连接异常,常见于网络状况不稳定。 | 建议稍后重试,若仍无法解决,请通过在线提单提交问题。 |
| 503 | 系统流控。 | 触发系统流控,请稍后重试。 |
| 504 | 请求连接超时,常见于网络状况不稳定。 | 建议稍后重试,若仍无法解决,请通过在线提单提交问题。 |
| 590 | 服务内部错误。 | 请通过在线提单提交问题。 |
| 业务响应主错误码 | 业务响应子错误码 | 描述 | 解决方法 |
|---|---|---|---|
| 1101 | 20002 | client_id格式不正确。 | 检查client_id是否满足正则:^[0-9]{1,64}$。 |
| 1101 | 20041 | scope格式不正确或数量超过150个。 | - 检查scope参数是否满足正则:^[0-9a-zA-Z:/\\.\u0020]+$。 - 检查scope数量是否超过150个。 |
| 1101 | 20042 | 无效的scope。 | 入参scope存在伪造值,请参照参数说明,传入正确的参数。 |
| 1101 | 20154 | refresh_token中的client_id和入参不一致。 | 检查入参client_id是否与配置Client ID中的值一致。 |
| 1101 | 20171 | client_secret为空。 | 请按照接口参数的要求,传入正确的client_secret参数。 |
| 1101 | 20172 | client_secret格式不正确。 | 检查client_secret格式是否满足正则:^[0-9a-zA-Z=/\\+]+$。 |
| 1101 | 20182 | grant_type值不正确。 | grant_type可选值如下: - “authorization_code”:该场景用于获取用户级凭证。 - “device_code”:该场景用于扫码授权登录-获取用户级凭证。 - “refresh_token”: 该场景用于刷新用户级凭证。 - “client_credentials”:该场景用于获取应用级凭证。 当前场景请固定传”refresh_token“。 |
| 1101 | 20192 | refresh_token格式不正确。 | refresh_token参数格式需要满足正则:^[0-9a-zA-Z=/\\+]+$。 |
| 1102 | 20001 | client_id为空。 | 请按照接口参数的要求,传入正确的client_id参数。 |
| 1102 | 20181 | grant_type为空。 | grant_type可选值如下: - “authorization_code”:该场景用于获取用户级凭证。 - “device_code”:该场景用于扫码授权登录-获取用户级凭证。 - “refresh_token”: 该场景用于刷新用户级凭证。 - “client_credentials”:该场景用于获取应用级凭证。 当前场景请固定传”refresh_token“。 |
| 1102 | 20191 | refresh_token为空。 | 请按照接口参数的要求,传入正确的refresh_token参数。 |
| 1203 | 11205 | refresh_token已过期。refresh_token的有效期为180天,超过有效期后将无法继续使用。 | 请引导用户重新授权,获取新的refresh_token。 |
| 1203 | 12303 | client_id在系统不存在。 | 请前往HarmonyOS 开发者中心确认client_id是否存在。 |
| 1203 | 12304 | 无效的client_secret。 | 入参client_id和client_secret不匹配导致,请检查参数。 |
| 1203 | 31202 | refresh_token解析失败。 | refresh_token不是一个正确有效的数据,请检查refresh_token参数。 |
| 1203 | 31204 | refresh_token已失效。正常refresh_token有效期为180天,但是由于用户的行为(如更改密码、取消应用的授权等行为),导致华为服务器提前使已颁发的refresh_token失效。 | 请引导用户重新授权,获取新的refresh_token。 |
| 1203 | 500 | 系统内部错误。 | 系统内部处理错误,建议打印错误码信息,并请通过在线提单提交问题。 |
| 1203 | 1203 | 系统未知异常。 | 系统未知异常,建议打印错误码信息,并请通过在线提单提交问题。 |