2017-12-05 11 views
2

인증 된 API 요청으로 GDAX Exchange에서 데이터를 가져 오려고합니다. 간단한 계좌 잔액 확인부터 시작하겠습니다.Java GDAX 인증 된 REST 요청 HTTP GET 오류 400

나는 약 8 시간 동안 내 코드를 수정 해 왔으며 400 응답 이외의 것을 얻을 수없는 것으로 보입니다. 아무도 제가 잘못하고있는 것을 이해하도록 도와 줄 수 있습니까?

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

모든 REST 요청은 다음과 같은 헤더를 포함해야합니다 : 문자열로

  • CB-ACCESS-KEY API 키.
  • CB-ACCESS-SIGN base64로 인코딩 된 서명입니다 (메시지 서명 참조).
  • CB-ACCESS-TIMESTAMP 요청한 시간 소인.
  • CB-ACCESS-PASSPHRASE API 키를 만들 때 지정한 암호입니다.

모든 요청 본문에는 콘텐츠 유형 application/json이 있고 유효한 JSON이어야합니다.

~

CB-ACCESS-SIGN 헤더 prehash 문자열 소인 +있어서 + requestPath + 본체 (의베이스 64 디코딩 된 비밀 키를 사용하여 SHA256 HMAC를 생성함으로써 생성된다 + 문자열 연결을 나타냄) 및 출력을 base64 인코딩합니다. 타임 스탬프 값은 CB-ACCESS-TIMESTAMP 헤더와 같습니다.

본문은 요청 본문 문자열이거나 요청 본문이 없으면 생략됩니다 (일반적으로 GET 요청 용).

대문자 여야합니다.

~

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

답변

2
+0

안녕 @Shawn 당신이 여기 링크 대신 코드를 게시하시기 바랍니다 수 Create Headers은 링크가 미래에 변경할 수 있습니다 : 여기

은 정확하게 당신이 뭘 하려는지의 예입니다 . –

+0

여기에 제공된 코드는 REST 요청에 Spring을 사용합니다. "순수한"자바로 어떻게 할 수 있습니까? – Ben