Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private JSONObject getAuthenticatedData() throws NoSuchAlgorithmException
- {
- try {
- String timestamp= Instant.now().getEpochSecond()+"";
- String accessSign = getAccess(timestamp);
- String apiKey = properties.getProperty("key");
- String passphrase = properties.getProperty("passphrase");
- System.out.println("$: "+accessSign);
- URL url = new URL("https://" + properties.getProperty("host") + "/accounts");
- HttpURLConnection con = (HttpURLConnection) url.openConnection();
- con.setRequestMethod("GET");
- con.setRequestProperty("CB-ACCESS-KEY", apiKey);
- con.setRequestProperty("CB-ACCESS-SIGN", accessSign);
- con.setRequestProperty("CB-ACCESS-PASSPHRASE", passphrase);
- con.setRequestProperty("CB-ACCESS-TIMESTAMP", timestamp);
- 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;
- }
- private String getAccess(String timestamp) throws NoSuchAlgorithmException, InvalidKeyException {
- String secret = properties.getProperty("secret");
- String prehash = timestamp+"GET"+"/accounts";
- Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
- byte[] secretDecoded = Base64.getDecoder().decode(secret);
- SecretKeySpec secret_key = new SecretKeySpec(secretDecoded, "HmacSHA256");
- sha256_HMAC.init(secret_key);
- return Base64.getEncoder().encodeToString(sha256_HMAC.doFinal(prehash.getBytes()));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement