【发布时间】:2020-06-16 05:24:29
【问题描述】:
我正在开发一个需要使用 Coinbase API v2 在两个钱包之间进行转账的项目。当我调用 API (https://api.coinbase.com/v2/accounts/:account_id/transactions) 时,我收到一个带有空正文的 200 Success 响应代码。这很好,但我的帐户上没有发生实际转移。 API 文档建议响应代码应为 201,收据显示交易的所有相关信息。这些都没有发生。
预期的参数是:
- 类型(在这种情况下,它始终是“转移”)
- 收件人(帐户 ID)
- 金额(双精度值)
- 货币(转账金额的货币单位)
我系统地更改了我的请求的每个变量、每个正文/标题元素、更改帐户 ID(往返)、输入无效货币、金额等。每次都返回一个 404 Bad Request,告诉我在哪里错误是。我还在其他点进行了其他几个 API 调用,每个都需要授权标头,并且这些调用传递并返回预期的数据。这意味着我当前进行 API 调用的设置是正确的,至少在代码方面是正确的。
这是我目前的调用方法:
主要:
JSONObject j = APICallBuilder.convertCurrency(CurrencyHandler.findCurrencyID("ETH"), CurrencyHandler.findCurrencyID("BTC"), 54);
System.out.println(j.toString(1));
货币处理程序:
public static String findCurrencyID(String code) {
String ID = "Invalid code";
for(int i = 0; i < currencyArray.length; i++) {
if(currencyArray[i].getCode().equals(code)) {
ID = currencyArray[i].getID();
}
}
return ID;
}
我直接从https://api.coinbase.com/v2/accounts 调用中获取 ID 值,并且我确信它是正确的,因为不正确的值(to 或 from)会导致 404 Bad Request 响应。我通过保留与ID,但用另一个字符替换单个字符(确保字符在 0-f 之间,就像原始 ID 一样)。
API 调用:
public static JSONObject convertCurrency(String fromCode, String toCode, double amount) {
String url = requests.getJSONObject("convert").getString("requestPath");
String requestPath = "/v2/accounts/${var}/transactions";
String method = "POST";
String timestamp = getEpochTime();
url = url.replace("${var}", fromCode);
requestPath = requestPath.replace("${var}", fromCode);
JSONObject bodyFormatter = new JSONObject();
bodyFormatter.put("type", "transfer");
bodyFormatter.put("to", toCode);
bodyFormatter.put("amount", amount);
bodyFormatter.put("currency", "USD");
String body = bodyFormatter.toString();
String header = HeaderGenerator.getHMACHeader(secretKey, timestamp, method, requestPath, body);
RequestBody rb = RequestBody.create(JSON, bodyFormatter.toString());
Request request = new Request.Builder()
.addHeader(CB_ACCESS_KEY, accessKey)
.addHeader(CB_ACCESS_SIGN, header)
.addHeader(CB_ACCESS_TIMESTAMP, timestamp)
.addHeader(CB_VERSION, getDate())
.addHeader("Accept", "application/json")
.addHeader("Content-Type", "application/json")
.url(url)
.post(rb)
.build();
Response res;
JSONObject par = null;
try {
res = APICommunicator.sendRequest(request);
String data = res.body().string();
System.out.println(res.code());
par = new JSONObject(data);
} catch (IOException e) {
e.printStackTrace();
ErrorLogger.logException(e);
}
return par;
}
我缺少的 API 有什么问题吗?我已经多次阅读整个文档,但我找不到发生这种情况的方式或原因。
【问题讨论】:
-
您的问题 jlars789 找到答案了吗?
-
我在使用 python 方法时遇到了同样的问题——响应代码为 200,但没有响应文本(更准确地说,响应为
{}),而其他端点请求正常运行。跨度> -
您找到答案了吗?我也有同样的问题。
标签: java coinbase-api