承载协议 | HTTPS POST |
|---|---|
接口方向 | 服务商服务器 -> 华为服务器 |
接口URL | https://connect-api.cloud.huawei.com/api/oauth2/v1/token |
数据格式 | 请求:Content-Type: application/json 响应:Content-Type: application/json |
参数名称 | 必选(M)/可选(O) | 数据类型 | 参数说明 |
|---|---|---|---|
grant_type | M | String(256) | 固定为client_credentials。 |
client_id | M | String(256) | API客户端ID。 创建第三方平台成功后系统自动分配的客户端ID,可在第三方管理平台“开发配置 > 开发资料设置”页面中获取,详情请参见接口访问凭证(平台访问凭据)。 |
client_secret | M | String(2048) | API客户端密钥。 创建第三方平台成功后系统自动生成的客户端密钥,可在第三方管理平台“开发配置 > 开发资料设置”页面中获取,详情请参见接口访问凭证(平台访问凭据)。 |
{
"grant_type":"client_credentials",
"client_id":"26********20",
"client_secret":"************************"
} 返回值为JSON格式的字符串,包含参数如下。
参数名称 | 数据类型 | 参数说明 |
|---|---|---|
access_token | String | 认证Token,用于第三方管理平台API接口调用。 此参数只在获取成功时返回。 |
expires_in | Long | access_token的有效期。 您需要在过期时间到达时重新调用本接口获取新的access_token。 有效期为48小时,如果在有效期内再次调用接口获取access_token时,新老access_token都是有效的。 此参数只在获取成功时返回。 单位:秒 |
ret | 包含返回码及描述信息。 返回码详情请参见错误码。 |
参数 | 类型 | 说明 |
|---|---|---|
code | Integer | 返回码。 |
msg | String | 返回码描述信息。 |
public static String getToken(String domain, String clientId, String clientSecret) {
String token = null;
try {
HttpPost post = new HttpPost(domain + "/oauth2/v1/token");
JSONObject keyString = new JSONObject();
keyString.put("client_id", "18893***83957248");
keyString.put("client_secret", "B15B497B44E080EBE2C4DE4E74930***52409516B2A1A5C8F0FCD2C579A8EB14");
keyString.put("grant_type", "client_credentials");
StringEntity entity = new StringEntity(keyString.toString(), Charset.forName("UTF-8"));
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
post.setEntity(entity);
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpResponse response = httpClient.execute(post);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
BufferedReader br =
new BufferedReader(new InputStreamReader(response.getEntity().getContent(), Consts.UTF_8));
String result = br.readLine();
JSONObject object = JSON.parseObject(result);
token = object.getString("access_token");
}
post.releaseConnection();
httpClient.close();
} catch (Exception e) {
}
return token;
}