Skip to content

API 接入流程

默认规则

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

通讯流程

接入准备

本次接入准备事项
首次Lenovo 会分配安全的 clientId、clientSecret,以及开通接口权限
非首次提供历史 clientId,用于 Lenovo 增加新的接口权限

环境信息

环境URLclientIdclientSecret
测试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

请求参数

位置名称必填描述示例
HeaderContent-TypeY固定值application/x-www-form-urlencoded
HeaderAuthorizationYBase64(consumer-key:consumer-secret)Basic SHlFdUFzUm80WjlLdEJLRGpJNUdhOW1mVGZJYTpnZWdDTVFENkZtYlBuWDU2RjNzTW1VYWhtVVVh
Parametergrant_typeY固定值client_credentials

响应参数

位置名称必填类型描述示例
Bodyaccess_tokenYstringTokenaaf0d4d6-c541-34cd-a4e0-03da1cc4019d
BodyscopeYstring范围am_application_scope default
Bodytoken_typeYstring类型Bearer
Bodyexpires_inYnumber有效期(秒)600
  • curl 请求示例
bash
curl -k -d "grant_type=client_credentials" -u clientId:clientSecret https://api-cn-t.lenovo.com/uat/token
bash
curl -k -d "grant_type=client_credentials" \
            -H "Authorization: Basic Base64(clientId:clientSecret)" \
             https://api-cn-t.lenovo.com/uat/token
  • Postman 请求示例
  1. 选择POST请求方式,填入请求地址,增加参数grant_type=client_credentials

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

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

  4. 发送请求

  • 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 {
        OAuthClientRequest accessTokenRequest = OAuthClientRequest.tokenLocation(oauth2Url)
                .setGrantType(GrantType.CLIENT_CREDENTIALS)
                .setClientId(clientId)
                .setClientSecret(clientSecret)
                .buildQueryMessage();
        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": "aaf0d4d6-c541-34cd-a4e0-03da1cc4019d",
    "scope": "am_application_scope default",
    "token_type": "Bearer",
    "expires_in": 600
}

Token 使用方式

所有业务接口,请求需 Header 中携带 Token ,确保安全性

公共请求参数

参数位置参数名称描述备注
HeaderAuthorizationBearer + 空格 + TokenBearer aaf0d4d6-c541-34cd-a4e0-03da1cc4019d