概述
接口使用
域名
https://<环境ID>.ap-shanghai.tcb-api.tencentcloudapi.com
环境 ID 可前往 资源管理 页面获取。
鉴权机制
- 前往 腾讯云控制台 申请 SecretId + SecretKey。
- 使用 OAuth 2.0 鉴权方式换取 Access Token,token获取url为:
https://<环境ID>.ap-shanghai.tcb-api.tencentcloudapi.com/auth/v1/token/clientCredential
- 使用 Access Token 请求接口并在 Request Header 中加入
Authorization: Bearer <Access Token>
。
请求说明
- 所有接口均通过 HTTPS 进行通信,均使用 UTF-8 编码。
- 支持的 HTTP 请求方法:POST、GET、PUT、DELETE。
- Content-Type 类型:
application/json;utf-8
。
NodeJS代码示例
const Koa = require('koa');
const fetch = require('node-fetch');
const app = new Koa();
const EnvId = ''; // 环境 ID,例如 lowcode-2gay8jgh25
const SecretId = '';
const SecretKey = '';
// 域名
const domain = `https://${EnvId}.ap-shanghai.tcb-api.tencentcloudapi.com`;
app.use(async ctx => {
// 换取 AccessToken
const tokenResponse = await fetch(`${domain}/auth/v1/token/clientCredential`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Basic ${Buffer.from(`${SecretId}:${SecretKey}`).toString('base64')}`
},
body: JSON.stringify({
grant_type: 'client_credentials',
})
});
const { access_token } = await tokenResponse.json();
// 请求某个服务端 API
const queryResponse = await fetch(`${domain}/weda/odata/v1/prod/sys_user`, {
method: "GET",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${access_token}`
}
});
ctx.body = await queryResponse.json();
});
app.listen(3000);
Java代码示例
package com.example.odata;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.util.Base64Utils;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.HashMap;
import java.util.Map;
public class OpenApiClient {
// 获取 token
private String getToken(String envId, String secretId, String secretKey) {
String host = "https://" + envId + ".ap-shanghai.tcb-api.tencentcloudapi.com";
String url = host + "/auth/v1/token/clientCredential";
HttpPost httpPost = new HttpPost(url);
String basicKey = secretId + ":" + secretKey;
String authorizationKey = "Basic " + Base64Utils.encodeToString(basicKey.getBytes());
httpPost.addHeader("Authorization",authorizationKey);
httpPost.addHeader("Content-Type","application/json");
Map<String, String> body = new HashMap<>();
body.put("grant_type","client_credentials");
ObjectMapper mapper = new ObjectMapper();
try {
String bodyStr = mapper.writeValueAsString(body);
StringEntity requestBody = new StringEntity(bodyStr, "UTF-8");
httpPost.setEntity(requestBody);
} catch (Exception e) {
System.out.println(e.toString());
return "";
}
ResponseHandler<String> responseHandler = response -> {
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
return entity != null ? EntityUtils.toString(entity) : null;
} else {
throw new ClientProtocolException("Unexpected response status: " + status + EntityUtils.toString(response.getEntity()));
}
};
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
String responseBody = httpClient.execute(httpPost, responseHandler);
Map<String, Object> responseMap = mapper.readValue(responseBody, Map.class);
/*
{
"token_type": "Bearer",
"access_token": "",
"expires_in": 111,// 过期时间,单位为 s
}
*/
return "Bearer " + responseMap.get("access_token").toString();
} catch (Exception e) {
System.out.println(e.toString());
}
return "";
}
// GET: 获取数据源记录列表
private void GET(String token, String envId, String envType, String datasourceName) {
String host = "https://" + envId + ".ap-shanghai.tcb-api.tencentcloudapi.com";
String url = host + "/weda/odata/v1/" + envType + "/" + datasourceName;
HttpGet httpGet = new HttpGet(url);
httpGet.addHeader("Authorization", token);
httpGet.addHeader("Content-Type", "application/json");
ResponseHandler<String> responseHandler = response -> {
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
return entity != null ? EntityUtils.toString(entity) : null;
} else {
throw new ClientProtocolException("Unexpected response status: " + status + EntityUtils.toString(response.getEntity()));
}
};
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
String responseBody = httpClient.execute(httpGet, responseHandler);
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> responseMap = mapper.readValue(responseBody, Map.class);
// 打印 body
System.out.println(responseMap.toString());
} catch (Exception e) {
System.out.println(e.toString());
}
}
public static void main(String[] args) {
OpenApiClient openApiClient = new OpenApiClient();
// 真实环境 ID
String envId = "";
String secretId = "";
String secretKey = "";
// 数据模型标识
String datasourceName = "";
// 数据模型类型
String envType = "pre";
String token = openApiClient.getToken(envId, secretId, secretKey);
//查看记录
openApiClient.GET(token, envId, envType, datasourceName);
}
}
快速体验:使用 Postman 调用开放接口
- 打开 Postman 工具,添加一个 GET 请求。进入 Authorization 页面完成相应配置。
Access Token URL 参数设置,需要在域名后添加
/auth/v1/token/clientCredential
。示例:https://lowcode-8g171waac4be77f6.ap-shanghai.tcb-api.tencentcloudapi.com/auth/v1/token/clientCredential
。 - 进入 headers 页面,设置相应参数即可。
接口异常返回
接口异常返回有两种格式:
- 公共参数校验异常返回格式
{
"code": "INVALID_ACCESS_TOKEN",
"requestId": "79e7c0fc8286e",
"message": "access token verify failed: jwt expired"
}
错误码:
错误码 | 描述 |
---|---|
INVALID_ACCESS_TOKEN | TOKEN不正确或已过期 |
BAD_REQUEST | 请求数据格式异常 |
INVALID_HOST | HOST不合法 |
INVALID_REGION | 地域不匹配 |
INVALID_ENV | 错误的环境、环境未找到 |
INVALID_ENV_STATUS | 环境状态异常、不可用 |
INVALID_COMMON_PARAM | 公共参数错误 |
SIGN_PARAM_INVALID | 签名校验不通过 |
PERMISSION_DENIED | 权限拒绝 |
EXCEED_REQUEST_LIMIT | 资源超限 |
EXCEED_RATELIMIT | QPS超限 |
SYS_ERR | 系统错误 |
SERVER_TIMEOUT | 服务超时 |
- 业务接口异常返回格式:
{
"Response": {
"RequestId": "",
"Error": {
"Code": "InvalidParameter",
"Message": "参数错误"
}
}
}
错误码:
在具体接口中描述。