合作咨询
我们的专家服务团队将竭诚为您提供专业的合作咨询服务
在使用API客户端方式调用第三方管理平台API接口前,需要通过华为开放平台进行鉴权,并获取认证通过后的Token。
| 承载协议 | HTTPS POST |
|---|---|
| 接口方向 | 服务商服务器 -> 华为服务器 |
| 接口URL | https://connect-api.cloud.huawei.com/api/oauth2/v1/token |
| 数据格式 | 请求:Content-Type: application/json 响应:Content-Type: application/json |
- {
- "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 | 包含返回码及描述信息。 返回码详情请参见错误码。 |
CodeAndMsg
| 参数 | 类型 | 说明 |
|---|---|---|
| code | Integer | 返回码。 |
| msg | String | 返回码描述信息。 |
- {
- "access_token": "eyJhbGciOiJIUzU****************",
- "expires_in": 172800
- }
- 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;
- }