java检验 400_Java GDAX经过身份验证的REST请求HTTP GET错误400

我正在尝试使用经过身份验证的API请求从GDAX Exchange获取数据.我开始简单的帐户余额检查.

我一直在调整我的代码大约8个小时,似乎除了400响应之外似乎得不到任何东西.谁能帮我理解我做错了什么?

All REST requests must contain the following headers:

CB-ACCESS-KEY The api key as a string.

CB-ACCESS-SIGN The base64-encoded signature (see Signing a Message).

CB-ACCESS-TIMESTAMP A timestamp for your request.

CB-ACCESS-PASSPHRASE The passphrase you specified when creating the API key.

All request bodies should have content type application/json and be

valid 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. The timestamp value is the same as the

CB-ACCESS-TIMESTAMP header.

The body is the request body string or omitted if there is no request

body (typically for GET requests).

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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值