我收到403错误,所以我知道它可能与身份验证有关,但我似乎无法弄明白。我一直在关注网站https://bitbucket.org/nitrous/mtgox-api/overview上的指示,但无济于事。据我所知,该协议是:
BTCUSE/money/info
或其他内容),后跟空字符\0
,然后是您的帖子参数,并将它们组合成一个字符串。 所以在这个阶段,我有
params: "nonce=1375719059438000"
message: "BTCUSD/money/info nonce=1375719059438000"
所以我只是尝试进行信息调用,除了nonce之外不需要任何参数。因此,正如文档所述,我将请求属性“rest-key”设置为我的apikey并“rest-sign”到我的签名,然后完成帖子。但是我收到了403回复。我从这个网站得到的几个问题以及我在论坛中看到但从未得到过回答的问题是
\0
,我会让我相信他实际上是在逃避\
将\\0
放入字符串中,而不是\0
。有任何想法吗?如果您认为在堆栈溢出时发布我的代码会更好,请告诉我。 以下是我提出请求的代码:
public String queryMtGox(String method, HashMap<String, String> args) throws
IllegalStateException{
String path = "BTCUSD/money/" + method;
try {
String nonce = String.valueOf(System.currentTimeMillis())+"000";
String post_data = this.buildMtGoxQueryString(args);
if (post_data.contentEquals("")){
post_data+="nonce="+nonce;
}else post_data+="&nonce="+nonce;
String message = path +"\0"+post_data;
// args signature
Mac mac = Mac.getInstance("HmacSHA512");
SecretKeySpec secret_spec = new
SecretKeySpec(Base64.decodeBase64(this.goxsecret), "HmacSHA512");
mac.init(secret_spec);
String signature = Base64.encodeBase64String(mac.doFinal(message.getBytes()));
// build URL
URL queryUrl = new URL("https://data.mtgox.com/api/2/" + path);
// create connection
HttpsURLConnection connection = (HttpsURLConnection)queryUrl.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Rest-Key", this.goxkey);
connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible;
MtGoxTradeCLI)");
connection.setRequestProperty("Content-Type","application/x-www-form-
urlencoded");
connection.setRequestProperty("Rest-Sign", signature.replaceAll("\n", ""));
// write post
DataOutputStream outdata = new DataOutputStream(connection.getOutputStream());
outdata.write(post_data.getBytes());
outdata.close();
connection.disconnect();
int len;
// read inf
byte buffer[] = new byte[16384];
len = connection.getInputStream().read(buffer, 0, 16384);
System.out.print(new String(buffer, 0, len, "UTF-8"));
} catch (Exception ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
return "foo";
}
我当然在getInputStream()
收到错误。