Java - Unauthorized

Trying to connect to coinbase pro advanced, keep getting unauthorized. Confused as to what I’m doing wrong.

Code:

private void GetAccounts()
 {
	String timestamp = Instant.now().getEpochSecond() + "";
	String path = "/api/v3/brokerage/accounts" + "";
	String accessSign = getAccess(timestamp, "GET", path);		
	String apiKey = properties.getProperty("key");
	URL url = new URL("https://" + properties.getProperty("host")+ path);
	HttpURLConnection con = (HttpURLConnection) url.openConnection();
	con.setRequestMethod("GET");
	con.setRequestProperty("User-Agent","Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
    con.setRequestProperty("accept", "application/json");
    con.addRequestProperty("CB-ACCESS-KEY", apiKey);
    con.addRequestProperty("CB-ACCESS-SIGN", accessSign);
    con.addRequestProperty("CB-ACCESS-TIMESTAMP", timestamp);
    con.setConnectTimeout(5000);
    con.setReadTimeout(5000);
    String status = con.getResponseMessage();
    System.out.println(status);

 }
private static String getAccess(String timestamp, String method, String path)
			throws NoSuchAlgorithmException, InvalidKeyException {
		String secret = properties.getProperty("secret");
		String prehash = timestamp + method + path + "";
		//System.out.println("#" + prehash);
		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()));
		}

In advanced trade secret is not base64 encoded, also signature should not be base64 encoded.

1 Like