import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
public class CatFeeAPIExample {
private static final String API_KEY = "your_api_key"; // 请替换为您的API Key
private static final String API_SECRET = "your_api_secret"; // 请替换为您的API Secret
private static final String BASE_URL = "https://api.catfee.io";
public static void main(String[] args) throws Exception {
// 请求方法
String method = "POST"; // 可以根据需要修改为 "GET", "PUT", "DELETE" 等
// 示例:创建订单
String path = "/v1/order";
Map<String, String> queryParams = new HashMap<>();
queryParams.put("quantity", "65000");
queryParams.put("receiver", "TRON_ADDRESS");
queryParams.put("duration", "1h");
// 生成请求头
String timestamp = generateTimestamp();
String requestPath = buildRequestPath(path, queryParams);
String signature = generateSignature(timestamp, method, requestPath);
// 创建HTTP请求
String url = BASE_URL + requestPath;
HttpRequest request = createRequest(url, method, timestamp, signature);
// 发送请求并获取响应
HttpClient client = HttpClient.newHttpClient();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
// 打印响应
System.out.println("Response Code: " + response.statusCode());
System.out.println("Response Body: " + response.body());
// 处理可能的错误信息
if (response.statusCode() != 200) {
System.out.println("Error: " + response.body());
}
}
// 生成当前 UTC 时间戳(ISO 8601格式)
public static String generateTimestamp() {
return Instant.now().toString();
}
// 构建请求路径,包括查询参数
public static String buildRequestPath(String path, Map<String, String> queryParams) {
if (queryParams == null || queryParams.isEmpty()) {
return path;
}
String queryString = queryParams.entrySet().stream()
.map(entry -> entry.getKey() + "=" + entry.getValue())
.collect(Collectors.joining("&", "?", ""));
return path + queryString;
}
// 生成签名
public static String generateSignature(String timestamp, String method, String requestPath) throws Exception {
String signString = timestamp + method + requestPath;
return hmacSHA256(signString, API_SECRET);
}
// 使用 HMAC-SHA256 算法生成签名
public static String hmacSHA256(String data, String secret) throws Exception {
Mac sha256Hmac = Mac.getInstance("HmacSHA256");
SecretKeySpec secretKey = new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
sha256Hmac.init(secretKey);
byte[] hash = sha256Hmac.doFinal(data.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(hash);
}
// 创建 HTTP 请求,支持 GET, POST, PUT, DELETE
public static HttpRequest createRequest(String url, String method, String timestamp, String signature) {
HttpRequest.Builder requestBuilder = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("Content-Type", "application/json")
.header("CF-ACCESS-KEY", API_KEY)
.header("CF-ACCESS-SIGN", signature)
.header("CF-ACCESS-TIMESTAMP", timestamp);
switch (method.toUpperCase()) {
case "POST":
requestBuilder.POST(HttpRequest.BodyPublishers.noBody());
break;
case "PUT":
requestBuilder.PUT(HttpRequest.BodyPublishers.noBody());
break;
case "DELETE":
requestBuilder.DELETE();
break;
case "GET":
requestBuilder.GET();
break;
default:
throw new UnsupportedOperationException("Unsupported HTTP method: " + method);
}
return requestBuilder.build();
}
}
此示例展示了如何在 Java 中使用 CatFee.IO Rest API 进行安全的 API 调用。通过使用 HMAC-SHA256 签名和正确的查询参数处理,确保请求的有效性和安全性。您可以根据需要调整代码,进行不同的操作,支持 GET
、POST
、PUT
和 DELETE
方法。