· 8 years ago · Dec 26, 2017, 10:30 PM
1private static JsonObject getAuthenticatedData() {
2 try {
3
4 String accessSign = getAccess();
5 System.setProperty("http.agent", "Java Client");
6
7 URL url = new URL("https://api.gdax.com/accounts");
8 HttpURLConnection con = (HttpURLConnection) url.openConnection();
9 con.setRequestMethod("GET");
10
11 con.setRequestProperty("CB-ACCESS-KEY", "accesskey");
12 con.setRequestProperty("CB-ACCESS-SIGN", accessSign);
13 con.setRequestProperty("CB-ACCESS-TIMESTAMP", ""+System.currentTimeMillis() / 1000L);
14 con.setRequestProperty("CB-ACCESS-PASSPHRASE", "passphrase");
15 con.setRequestProperty("Content-Type", "application/json");
16
17
18 con.setConnectTimeout(5000);
19 con.setReadTimeout(5000);
20
21 String status = con.getResponseMessage();
22 System.out.println(status);
23
24 BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
25 String inputLine;
26 StringBuffer content = new StringBuffer();
27 while ((inputLine = in.readLine()) != null) {
28 content.append(inputLine);
29 }
30 System.out.println(content);
31 in.close();
32
33 con.disconnect();
34
35 }catch(Exception e) {
36 e.printStackTrace();
37 }
38 return null;
39
40
41 }
42
43public static String getAccess() {
44
45 //Set the Secret
46 String secret = "secret==";
47 //Build the PreHash
48 String prehash = Instant.now().toEpochMilli()+"GET"+"/accounts";
49 String hash = null;
50 try {
51
52 Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
53 SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
54 sha256_HMAC.init(secret_key);
55
56 hash = Base64.encodeBase64String(sha256_HMAC.doFinal(prehash.getBytes("UTF-8")));
57 hash = hash.replace("n", "");
58 System.out.println(hash);
59 }
60 catch (Exception e){
61 e.printStackTrace();
62 }
63 return hash;
64}