API 接入流程
默认规则
- 使用
HTTPS作为传输协议,默认POST请求 API遵循REST风格,默认使用JSON作为报文传输协议- 注意 UrlEncode 情况
- 接口限制:RequestBody 默认小于 500 条并且小于 1Mb
- 默认超时时间 60s
通讯流程

接入准备
| 本次接入 | 准备事项 |
|---|---|
| 首次 | Lenovo 会分配安全的 clientId、clientSecret,以及开通接口权限 |
| 非首次 | 提供历史 clientId,用于 Lenovo 增加新的接口权限 |
环境信息
| 环境 | URL | clientId | clientSecret |
|---|---|---|---|
| 测试 | https://api-cn-t.lenovo.com/uat | 申请后告知 | 申请后告知 |
| 正式 | https://api-cn.lenovo.com | 申请后告知 | 申请后告知 |
获取 Token 鉴权
Token 采用 Oauth2 的 client_credentials 机制,并且有过期机制,请注意缓存、更新,并避免频繁刷新
| 环境 | 鉴权 URL |
|---|---|
| 测试 | https://api-cn-t.lenovo.com/uat/token |
| 正式 | https://api-cn.lenovo.com/token |
请求参数
| 位置 | 名称 | 必填 | 描述 | 示例 |
|---|---|---|---|---|
| Header | Content-Type | Y | 固定值 | application/x-www-form-urlencoded |
| Header | Authorization | Y | Base64(consumer-key:consumer-secret) | Basic S*************************** |
| Parameter | grant_type | Y | 固定值 | client_credentials |
响应参数
| 位置 | 名称 | 必填 | 类型 | 描述 | 示例 |
|---|---|---|---|---|---|
| Body | access_token | Y | string | Token | aa**************** |
| Body | scope | Y | string | 范围 | am_application_scope default |
| Body | token_type | Y | string | 类型 | Bearer |
| Body | expires_in | Y | number | 有效期(秒) | 600 |
- curl 请求示例
bash
curl -k -d "grant_type=client_credentials" -u clientId:clientSecret https://api-cn-t.lenovo.com/uat/tokenbash
curl -k -d "grant_type=client_credentials" \
-H "Authorization: Basic Base64(clientId:clientSecret)" \
https://api-cn-t.lenovo.com/uat/token- Postman 请求示例
选择POST请求方式,填入请求地址,增加参数grant_type=client_credentials

添加 Authorization, Auth Type 选择 Basic Auth,填写 clientId 和 clientSecret

添加header:Content-Type: application/x-www-form-urlencoded

发送请求

- Java 请求示例
java
import org.apache.oltu.oauth2.client.OAuthClient;
import org.apache.oltu.oauth2.client.URLConnectionClient;
import org.apache.oltu.oauth2.client.request.OAuthClientRequest;
import org.apache.oltu.oauth2.client.response.OAuthAccessTokenResponse;
import org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse;
import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
import org.apache.oltu.oauth2.common.message.types.GrantType;
public static OAuthAccessTokenResponse getToken(String clientId, String clientSecret, String oauth2Url) {
try {
String credentials = clientId + ":" + clientSecret;
String encodedCredentials = Base64.encodeBase64String(credentials.getBytes(StandardCharsets.UTF_8));
OAuthClientRequest accessTokenRequest = OAuthClientRequest.tokenLocation(oauth2Url)
.setGrantType(GrantType.CLIENT_CREDENTIALS)
.buildBodyMessage();
accessTokenRequest.addHeader("Authorization", "Basic " + encodedCredentials);
OAuthClient client = new OAuthClient(new URLConnectionClient());
OAuthAccessTokenResponse oauthResponse = client.accessToken(accessTokenRequest, OAuthJSONAccessTokenResponse.class);
return oauthResponse;
} catch (OAuthSystemException | OAuthProblemException e) {
log.error("Oauth2Utils getToken Error", e);
}
return null;
}- 响应示例
json
{
"access_token": "aa****************",
"scope": "am_application_scope default",
"token_type": "Bearer",
"expires_in": 600
}Token 使用方式
所有业务接口,请求需 Header 中携带 Token ,确保安全性
公共请求参数
| 参数位置 | 参数名称 | 描述 | 备注 |
|---|---|---|---|
| Header | Authorization | Bearer + 空格 + Token | Bearer aa**************** |
