对接详情
使用API的方式
- 在请求头中添加 AccessKeyId 参数
数据签名
使用下面示例代码将参数签名, 签名后的值添加在请求头的 Signature
或者在请求头中添加 AccessKeySecret 参数(不推荐)
示例代码
import com.alibaba.fastjson2.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.AbstractResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.util.FileCopyUtils;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
public class Sign {
public final static String AccessKeyId = "AccessKeyId";
public final static String Signature = "Signature";
// TODO 填写你的 accessKeyId
private final static String accessKeyId = "";
// TODO 填写你的 accessKeySecret
private final static String accessKeySecret = "";
public static void main(String[] args) throws IOException {
// 查询资产信息
accountInfo();
// 查询模板
// artworkTemplateList();
// 上传模板
// artworkCreateTemplate();
// 删除模板
// artworkDeleteTemplate();
// 上传头像
// artworkCreateAvatar();
// 查询头像
// artworkQueryAvatar();
// 校验头像
// artworkCheckAvatar();
// 提交任务
// artworkSubmitTask();
// 查询任务
// artworkQueryTask();
}
private static void accountInfo() throws IOException {
HttpGet request = new HttpGet("https://api.01aigc.com/aicamera/account/info");
Map<String, Object> param = Map.of();
request.addHeader(AccessKeyId, accessKeyId);
request.addHeader(Signature, requestSign(param, accessKeySecret));
String response = new String(callHttp(request));
System.out.println(response);
}
private static void artworkTemplateList() throws IOException {
HttpGet request = new HttpGet("https://api.01aigc.com/aicamera/artwork/template_list?offset=0&size=10");
Map<String, Object> param = Map.of(
"offset", 0,
"size", 10
);
request.addHeader(AccessKeyId, accessKeyId);
request.addHeader(Signature, requestSign(param, accessKeySecret));
String response = new String(callHttp(request));
System.out.println(response);
}
private static void artworkCreateTemplate() throws IOException {
HttpPost request = new HttpPost("https://api.01aigc.com/aicamera/artwork/create_template");
// Map<String, Object> param = Map.of(
// "packageName", "单人模板测试",
// "imageURL", "https://api.01aigc.com/template/1.jpg",
// "peopleNum", 1
//// ,"version", "4.0"
// );
Map<String, Object> param = Map.of(
"packageName", "双人模板测试",
"imageURL", "https://api.01aigc.com/template/2.jpg",
"peopleNum", 2
// ,"version", "4.0"
);
request.setHeader(HttpHeaders.ACCEPT, ContentType.APPLICATION_JSON.getMimeType());
request.setEntity(new StringEntity(JSONObject.toJSONString(param), ContentType.APPLICATION_JSON));
request.addHeader(AccessKeyId, accessKeyId);
request.addHeader(Signature, requestSign(param, accessKeySecret));
String response = new String(callHttp(request));
System.out.println(response);
}
private static void artworkDeleteTemplate() throws IOException {
HttpPost request = new HttpPost("https://api.01aigc.com/aicamera/artwork/delete_template");
Map<String, Object> param = Map.of(
"templateId", "5cbbaf7a25f219fc955116dce6a34720"
);
request.setHeader(HttpHeaders.ACCEPT, ContentType.APPLICATION_JSON.getMimeType());
request.setEntity(new StringEntity(JSONObject.toJSONString(param), ContentType.APPLICATION_JSON));
request.addHeader(AccessKeyId, accessKeyId);
request.addHeader(Signature, requestSign(param, accessKeySecret));
String response = new String(callHttp(request));
System.out.println(response);
}
private static void artworkCreateAvatar() throws IOException {
HttpPost request = new HttpPost("https://api.01aigc.com/aicamera/artwork/create_avatar");
Map<String, Object> param = Map.of(
"imageURL", "https://api.01aigc.com/template/avatar.jpg"
// ,"version", "4.0"
);
request.setHeader(HttpHeaders.ACCEPT, ContentType.APPLICATION_JSON.getMimeType());
request.setEntity(new StringEntity(JSONObject.toJSONString(param), ContentType.APPLICATION_JSON));
request.addHeader(AccessKeyId, accessKeyId);
request.addHeader(Signature, requestSign(param, accessKeySecret));
String response = new String(callHttp(request));
System.out.println(response);
}
private static void artworkQueryAvatar() throws IOException {
String avatarId = "0f42e87ed135c8e57fca0151fbaaad6b";
HttpGet request = new HttpGet("https://api.01aigc.com/aicamera/artwork/query_avatar?avatarId=" + avatarId);
Map<String, Object> param = Map.of(
"avatarId", avatarId
);
request.addHeader(AccessKeyId, accessKeyId);
request.addHeader(Signature, requestSign(param, accessKeySecret));
String response = new String(callHttp(request));
System.out.println(response);
}
private static void artworkCheckAvatar() throws IOException {
HttpPost request = new HttpPost("https://api.01aigc.com/aicamera/artwork/check_avatar");
Map<String, Object> param = Map.of(
"avatarId", "690378b93977304793afcf23dc7393dc"
// ,"version", "4.0"
);
request.setHeader(HttpHeaders.ACCEPT, ContentType.APPLICATION_JSON.getMimeType());
request.setEntity(new StringEntity(JSONObject.toJSONString(param), ContentType.APPLICATION_JSON));
request.addHeader(AccessKeyId, accessKeyId);
request.addHeader(Signature, requestSign(param, accessKeySecret));
String response = new String(callHttp(request));
System.out.println(response);
}
private static void artworkSubmitTask() throws IOException {
HttpPost request = new HttpPost("https://api.01aigc.com/aicamera/artwork/submit_task");
// Map<String, Object> param = Map.of(
// "avatarList", List.of(
// Map.of("avatarId", "0f42e87ed135c8e57fca0151fbaaad6b")
// ),
// "templateList", List.of(
// Map.of("templateId", "208adfc05ab366b0af6e38465accf715"),
// Map.of("templateId", "0127c35e4ed3e035133edeb8e57b973e")
// ),
// "notifyURL", "https://api.01aigc.com/aicamera/artwork/test_notify"
//// ,"version", "4.0"
// );
Map<String, Object> param = Map.of(
"avatarList", List.of(
Map.of("avatarId", "0f42e87ed135c8e57fca0151fbaaad6b"),
Map.of("avatarId", "690378b93977304793afcf23dc7393dc")
),
"templateList", List.of(
Map.of("templateId", "682eec93b4fa00e438a404ea0f1282b8"),
Map.of("templateId", "805a161a60b6d36360ffdbed4acf8c56")
),
"notifyURL", "https://api.01aigc.com/aicamera/artwork/test_notify"
// ,"version", "4.0"
);
request.setHeader(HttpHeaders.ACCEPT, ContentType.APPLICATION_JSON.getMimeType());
request.setEntity(new StringEntity(JSONObject.toJSONString(param), ContentType.APPLICATION_JSON));
request.addHeader(AccessKeyId, accessKeyId);
request.addHeader(Signature, requestSign(param, accessKeySecret));
String response = new String(callHttp(request));
System.out.println(response);
}
private static void artworkQueryTask() throws IOException {
String taskId = "961dd574dc475349d628579e7e2f069a";
HttpGet request = new HttpGet("https://api.01aigc.com/aicamera/artwork/query_task?taskId=" + taskId);
Map<String, Object> param = Map.of(
"taskId", taskId
);
request.addHeader(AccessKeyId, accessKeyId);
request.addHeader(Signature, requestSign(param, accessKeySecret));
String response = new String(callHttp(request));
System.out.println(response);
}
public static byte[] callHttp(HttpUriRequest request) throws IOException {
try (CloseableHttpClient client = HttpClients.createDefault()) {
return client.execute(request, new AbstractResponseHandler<>() {
@Override
public byte[] handleResponse(final HttpResponse response) throws IOException {
final HttpEntity entity = response.getEntity();
byte[] result = this.handleEntity(entity);
if (response.getStatusLine().getStatusCode() >= HttpStatus.SC_MULTIPLE_CHOICES) {
EntityUtils.consume(entity);
throw new RuntimeException(
null == result ? response.getStatusLine().getReasonPhrase() : new String(result));
}
return result;
}
@Override
public byte[] handleEntity(HttpEntity entity) throws IOException {
return null == entity ? null : FileCopyUtils.copyToByteArray(entity.getContent());
}
});
}
}
public static String requestSign(Map<String, Object> paramsMap, String accessKeySecret) {
List<String> paramsArr = new ArrayList<>();
if (null != paramsMap) {
for (Map.Entry<String, Object> entry : paramsMap.entrySet()) {
String value = entry.getValue().toString();
value = value.trim();
if (value.startsWith("\"") && value.endsWith("\"") && value.length() > 1) {
value = value.substring(1, value.length() - 1);
}
value = value.trim();
if (value.equals("") || value.equals("null")) {
continue;
}
paramsArr.add(value);
}
}
paramsArr.add(accessKeySecret);
Collections.sort(paramsArr);
StringBuilder signStr = new StringBuilder();
String sep = "";
for (String s : paramsArr) {
signStr.append(sep).append(s);
sep = "&";
}
return md5FromStr(signStr.toString());
}
private static String md5FromStr(String inStr) {
MessageDigest md5;
try {
md5 = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return "";
}
byte[] byteArray = inStr.getBytes(StandardCharsets.UTF_8);
byte[] md5Bytes = md5.digest(byteArray);
StringBuilder hexValue = new StringBuilder();
for (byte md5Byte : md5Bytes) {
int val = ((int) md5Byte) & 0xff;
if (val < 16) {
hexValue.append("0");
}
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
}
}
最后修改时间: 1 年前