简体   繁体   English

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

[英]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: 以下是您正在尝试做的事情的示例:

Create Signature 创建签名

Create Headers 创建标题

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.

相关问题 从Java应用程序到TFS REST API的HTTP PATCH请求收到400(错误请求)错误 - HTTP PATCH request to TFS REST API from java application getting 400 (bad request) error Parse.com REST API错误代码400:从Java HTTP请求到云功能的错误请求 - Parse.com REST API Error Code 400: Bad Request from Java HTTP Request to Cloud Function 使用android studio的gdax rest api请求 - gdax rest api request using android studio Java WebServlet API:HTTP 400错误-错误的请求 - Java webservlet API: HTTP 400 error - bad request HTTP错误400错误的请求 - HTTP Error 400 Bad request 如何在 Java 中获取 HTTP 错误 400 上的响应 json - How to get the response json on HTTP error 400 in Java Java APIRest - 使用 GET 方法和 JWT 的 Postman 错误 400 错误请求 - Java APIRest - Postman error 400 bad request with GET method and JWT 使用Spring Mvc的REST请求上的HTTP 400错误请求 - HTTP 400 Bad Request on REST request using Spring Mvc 获得 HTTP 状态 400 – rest 中的发布请求中的错误请求得到保证 - Getting HTTP Status 400 – Bad Request in post request in rest assured 使用Java向JIRA发送HTTP POST请求会产生400错误的请求错误,curl正常运行 - HTTP POST request to JIRA using Java yields 400 bad request error, curl works fine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM