0
我試圖讓我的GDAX平衡,但我想我能不能正確處理Java中的編碼/解碼HMAC SHA-256編碼/解碼。 的API參考這裏:https://docs.gdax.com/#authentication,和我想要做的部分根據賬戶 - >列出帳戶(只需要降低一點從上面的鏈接滾動)只得到平衡。Base64和Java中
這裏是我的代碼。儘管我在Google和這裏進行了多少搜索,但似乎無法使其工作。我一直從服務器收到400錯誤,意思是「錯誤的請求 - 無效的請求格式」。
// Decode the secret key
byte[] decodedSecret;
try
{
decodedSecret = Base64.decode(SECRET_KEY);
}
catch (Base64DecodingException ex)
{
System.out.println("Failed to decode secret key.");
return null;
}
// Make the header parameters
long timestamp = (new GregorianCalendar()).getTimeInMillis()/1000;
String preSign = "" + timestamp + "GET" + BASE_URL + "/accounts";
byte[] encodedhash;
try
{
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
try
{
sha256_HMAC.init(new SecretKeySpec(decodedSecret, "HmacSHA256"));
}
catch (InvalidKeyException ex)
{
System.out.println("Failed due to invalid key exception.");
System.out.println(ex.getMessage());
return null;
}
encodedhash = sha256_HMAC.doFinal(preSign.getBytes());
}
catch (NoSuchAlgorithmException ex)
{
System.out.println("Failed to make SHA-256 encode because of no such algorithm.");
return null;
}
HashMap<String, String> parameters = new HashMap<>();
parameters.put("CB-ACCESS-KEY", API_KEY);
parameters.put("CB-ACCESS-SIGN", Base64.encode(encodedhash));
parameters.put("CB-ACCESS-TIMESTAMP", "" + timestamp);
parameters.put("CB-ACCESS-PASSPHRASE", PASSPHRASE);
// Send the request
String response = sendGet(BASE_URL + "/accounts", parameters);
這裏是我在sendGet()中的代碼,以防萬一這是問題所在。我知道它沒有參數,但我不知道它是否正確使用參數,因爲我無法使它工作。
// Set up the connection
URL url = null;
try
{
url = new URL(urlStr);
}
catch (MalformedURLException ex)
{
return null;
}
HttpURLConnection con;
try
{
con = (HttpURLConnection) url.openConnection();
}
catch (IOException ex)
{
System.out.println("Returning null because of failure to open connection.");
return null;
}
try
{
con.setRequestMethod("GET");
}
catch (ProtocolException ex) {}
if (parameters != null) // if there are parameters to add to the connection
{
for (Map.Entry<String, String> pair : parameters.entrySet()) // for each pair in parameters
{
try
{
con.addRequestProperty(pair.getKey(), pair.getValue());
}
catch (Exception ex)
{
System.out.println("Failed to add " + pair.getKey() + ".");
}
}
}
// Get the response
int responseCode;
try
{
responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
StringBuilder responseBldr = new StringBuilder();
String line;
while ((line = in.readLine()) != null) // while we have read another line
{
responseBldr.append(line);
}
in.close();
return responseBldr.toString();
}
catch (IOException ex)
{
System.out.println("Returning null from network IOException.");
System.out.println(ex.getMessage());
return null;
}
您是否設置了內容類型標題? – algrid
我沒有設置它。謝謝你。不過,它仍然給我答覆400。這是我做了添加的內容類型:con.addRequestProperty(「內容類型」,則contentType); – cmasupra
另外'preSign'可能不應該包含'BASE_URL'。 – algrid