<p>Can you try to add/modify with the with the following lines of codes in your methods:</p>

<pre>
private static JsonObject getAuthenticatedData() {

  String timestamp = Instant.now().getEpochSecond() + "";
  String accessSign = getAccess(timestamp);


  con.setRequestProperty("CB-ACCESS-TIMESTAMP", timestamp);
  con.setRequestProperty("accept", "application/json");
}
</pre>

<pre>
public static String getAccess(String timestamp) {

  String prehash = timestamp+"GET"+"/accounts";
  byte[] secretDecoded = Base64.getDecoder().decode(secret);
  SecretKeySpec secret_key = new SecretKeySpec(secretDecoded, "HmacSHA256");

  hash = Base64.getEncoder().encodeToString(sha256_HMAC.doFinal(prehash.getBytes()));

  return hash;
}
</pre>

CC BY-SA 3.0
2017-12-27T16:11:29.330Z
2017-12-27T16:11:29.330Z
Chak
<p>Trying to request private data on GDAX but keep getting bad request. Cannot figure out what I am doing wrong. Please help.</p>

<pre><code>private static JsonObject getAuthenticatedData() {
        try {

            String accessSign = getAccess();
            System.setProperty("http.agent", "Java Client");

            URL url = new URL("https://api.gdax.com/accounts");
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("GET");

            con.setRequestProperty("CB-ACCESS-KEY", "accesskey");
            con.setRequestProperty("CB-ACCESS-SIGN", accessSign);
            con.setRequestProperty("CB-ACCESS-TIMESTAMP", ""+System.currentTimeMillis() / 1000L);
            con.setRequestProperty("CB-ACCESS-PASSPHRASE", "passphrase");
            con.setRequestProperty("Content-Type", "application/json");


            con.setConnectTimeout(5000);
            con.setReadTimeout(5000);

            String status = con.getResponseMessage();
            System.out.println(status);

            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;


    }
</code></pre>

<p>And this:</p>

<pre><code>public static String getAccess() {

    //Set the Secret
    String secret = "secret==";
    //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("UTF-8")));
         hash = hash.replace("\n", "");
        System.out.println(hash);
       }
       catch (Exception e){
           e.printStackTrace();
       }
    return hash;   
}
</code></pre>

CC BY-SA 3.0
2017-12-26T23:35:04.910Z
What kind of errors do you get? In your getAccess() method, String prehash, shouldn't toEpochMilli() be in seconds?
Chak
CC BY-SA 3.0
2017-12-26T23:36:20.063Z
400 - BadRequest
X4RQ
CC BY-SA 3.0
2017-12-27T01:57:17.630Z
no matter what i do. always 400 bad request with no other description.
X4RQ
<java><gdax>
GDAX Java Bad Request
X4RQ
# GDAX Java Bad Request

(Asked by: username_0 on 2017-12-26)

Trying to request private data on GDAX but keep getting bad request. Cannot figure out what I am doing wrong. Please help.
private static JsonObject getAuthenticatedData() {
        try {

            String accessSign = getAccess();
            System.setProperty("http.agent", "Java Client");

            URL url = new URL("https://api.gdax.com/accounts");
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("GET");

            con.setRequestProperty("CB-ACCESS-KEY", "accesskey");
            con.setRequestProperty("CB-ACCESS-SIGN", accessSign);
            con.setRequestProperty("CB-ACCESS-TIMESTAMP", ""+System.currentTimeMillis() / 1000L);
            con.setRequestProperty("CB-ACCESS-PASSPHRASE", "passphrase");
            con.setRequestProperty("Content-Type", "application/json");


            con.setConnectTimeout(5000);
            con.setReadTimeout(5000);

            String status = con.getResponseMessage();
            System.out.println(status);

            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;


    }

And this:
public static String getAccess() {

    //Set the Secret
    String secret = "secret==";
    //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("UTF-8")));
         hash = hash.replace("\n", "");
        System.out.println(hash);
       }
       catch (Exception e){
           e.printStackTrace();
       }
    return hash;   
}

**Tags:** java, gdax

> **Comments:**
> **username_1**: What kind of errors do you get? In your getAccess() method, String prehash, shouldn't toEpochMilli() be in seconds?  
> **username_0**: 400 - BadRequest  
> **username_0**: no matter what i do. always 400 bad request with no other description.  

---

## 1 answers

### Answer by username_1 on 2017-12-27. Score: 1 | Accepted answer

Can you try to add/modify with the with the following lines of codes in your methods:

private static JsonObject getAuthenticatedData() {

  String timestamp = Instant.now().getEpochSecond() + "";
  String accessSign = getAccess(timestamp);


  con.setRequestProperty("CB-ACCESS-TIMESTAMP", timestamp);
  con.setRequestProperty("accept", "application/json");
}


public static String getAccess(String timestamp) {

  String prehash = timestamp+"GET"+"/accounts";
  byte[] secretDecoded = Base64.getDecoder().decode(secret);
  SecretKeySpec secret_key = new SecretKeySpec(secretDecoded, "HmacSHA256");

  hash = Base64.getEncoder().encodeToString(sha256_HMAC.doFinal(prehash.getBytes()));

  return hash;
}


---

