Java

GDAX Java 錯誤請求

  • December 27, 2017

嘗試在 GDAX 上請求私人數據,但不斷收到錯誤請求。無法弄清楚我做錯了什麼。請幫忙。

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;


   }

和這個:

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

您可以嘗試在您的方法中使用以下程式碼行添加/修改:

私有靜態 JsonObject getAuthenticatedData() {

字元串時間戳 = Instant.now().getEpochSecond() + "";
字元串 accessSign = getAccess(timestamp);


con.setRequestProperty("CB-ACCESS-TIMESTAMP", 時間戳);
con.setRequestProperty("接受", "應用程序/json");
}
公共靜態字元串getAccess(字元串時間戳){

字元串 prehash = 時間戳+"GET"+"/accounts";
byte[] secretDecoded = Base64.getDecoder().decode(secret);
SecretKeySpec secret_key = new SecretKeySpec(secretDecoded, "HmacSHA256");

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

返回雜湊;
}

引用自:https://bitcoin.stackexchange.com/questions/66637