[英]Java GDAX Authenticated REST Request HTTP GET Error 400
I am trying to GET data from GDAX Exchange with an authenticated API request. 我正在尝试使用经过身份验证的API请求从GDAX Exchange获取数据。 I'm starting with a simple account balance check.
我开始简单的帐户余额检查。
I've been tweaking my code for about 8 hours and can't seem to get anything other than a 400 response. 我一直在调整我的代码大约8个小时,似乎除了400响应之外似乎得不到任何东西。 Can anyone help me understand what I'm doing wrong?
谁能帮我理解我做错了什么?
https://docs.gdax.com/#authentication https://docs.gdax.com/#authentication
All REST requests must contain the following headers:
所有REST请求必须包含以下标头:
- CB-ACCESS-KEY The api key as a string.
CB-ACCESS-KEY api键作为字符串。
- CB-ACCESS-SIGN The base64-encoded signature (see Signing a Message).
CB-ACCESS-SIGN base64编码的签名(请参阅签名消息)。
- CB-ACCESS-TIMESTAMP A timestamp for your request.
CB-ACCESS-TIMESTAMP您的请求的时间戳。
- CB-ACCESS-PASSPHRASE The passphrase you specified when creating the API key.
CB-ACCESS-PASSPHRASE您在创建API密钥时指定的密码。
All request bodies should have content type application/json and be valid JSON.
所有请求主体都应该具有内容类型application / json并且是有效的JSON。
~ 〜
The CB-ACCESS-SIGN header is generated by creating a sha256 HMAC using the base64-decoded secret key on the prehash string timestamp + method + requestPath + body (where + represents string concatenation) and base64-encode the output.
CB-ACCESS-SIGN标头是通过在prehash字符串时间戳+ method + requestPath + body(其中+表示字符串连接)上使用base64解码的密钥创建sha256 HMAC并对输出进行base64编码来生成的。 The timestamp value is the same as the CB-ACCESS-TIMESTAMP header.
时间戳值与CB-ACCESS-TIMESTAMP头相同。
The body is the request body string or omitted if there is no request body (typically for GET requests).
如果没有请求主体(通常用于GET请求),则主体是请求主体字符串或省略。
The method should be UPPER CASE.
该方法应该是大写的。
~ 〜
private static JSONObject getAuthenticatedData() {
try {
String accessSign = getAccess();
URL url = new URL("https://api.gdax.com/accounts");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("CB-ACCESS-KEY", "d281dc......");
con.setRequestProperty("CB-ACCESS-SIGN", accessSign);
con.setRequestProperty("CB-ACCESS-TIMESTAMP", ""+System.currentTimeMillis() / 1000L);
con.setRequestProperty("CB-ACCESS-PASSPHRASE", "xxxxx.....");
con.setConnectTimeout(5000);
con.setReadTimeout(5000);
int status = con.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
System.out.println(content);
in.close();
con.disconnect();
}catch(Exception e) {
e.printStackTrace();
}
return null;
}
~ 〜
public static String getAccess() {
//Set the Secret
String secret = "xxxxxxx........";
//Build the PreHash
String prehash = Instant.now().toEpochMilli()+"GET"+"/accounts";
String hash = null;
try {
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
sha256_HMAC.init(secret_key);
hash = Base64.encodeBase64String(sha256_HMAC.doFinal(prehash.getBytes()));
System.out.println(hash);
}
catch (Exception e){
e.printStackTrace();
}
return hash;
}
You need to add request headers and request properties. 您需要添加请求标头和请求属性。
Here is and example of exactly what you are trying to do: 以下是您正在尝试做的事情的示例:
A 400 means that the request was malformed. 400表示请求格式错误。 In other words, the data stream sent by the client to the server didn't follow the rules.
换句话说,客户端发送到服务器的数据流不遵循规则。
So, something is wrong by your side. 所以,你身边出了点问题。 In official doc it is mentioned that you should request POST method but you have have request GET method.
在官方文档中提到您应该请求POST方法但是您有请求GET方法。
URL url = new URL("https://api.gdax.com/accounts");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
Hope, this may help! 希望,这可能会有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.