Java GDAX 身份验证的 REST 请求 HTTP GET 错误 400

标签 java json rest get http-headers

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

我已经调整我的代码大约 8 个小时了,除了 400 响应之外似乎什么也得不到。谁能帮助我了解我做错了什么?

https://docs.gdax.com/#authentication

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;   
}

最佳答案

您需要添加请求 header 和请求属性。

这是您正在尝试做的事情的示例:

Create Signature

Create Headers

关于Java GDAX 身份验证的 REST 请求 HTTP GET 错误 400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47662208/

相关文章:

java - 组合两个具有不同异常类型的函数(java 泛型)

javascript - 使用 JSON 中的 header 从 JSON 创建对象

Python REST API 发布列表

java - 在没有容器的情况下解决 EJB 依赖关系

java - 使带注释的困惑远离领域模型

json - 在 iex 中运行 elixir poison 命令

PHP 解码、更改和编码 JSON

javascript - Angular,将 json 发送到 API

java - GetRequest集会限制pageSize

java - 视频在本地系统中播放,但现在在远程系统的内联网中播放