# Pritunl Rest Api authentication using Java

**URL:** https://forum.pritunl.com/t/pritunl-rest-api-authentication-using-java/2999
**Category:** Pritunl
**Tags:** pritunl-endpoint, pritunl-client
**Created:** [January 26, 2025, 5:59pm UTC](https://forum.pritunl.com/t/pritunl-rest-api-authentication-using-java/2999 "2025-01-26T17:59:10Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Devour2005](https://forum.pritunl.com/user_avatar/forum.pritunl.com/devour2005/32/1166_2.png) [@Devour2005](https://forum.pritunl.com/u/Devour2005)
#### Post date: [January 26, 2025, 5:59pm UTC](https://forum.pritunl.com/t/pritunl-rest-api-authentication-using-java/2999/1 "2025-01-26T17:59:10Z")

</div>

Need help with authentication and access to Pritunl API. I get 401 Unauthorized and I think it’s happening because I missed something when converted python to java. If somebody could share his/her example or give a hint how to fix an issue. Thanks in advance.

My example:

```auto
        String path = "/organization";
        String url = API_URL + path;
        String timestamp = String.valueOf(Instant.now().getEpochSecond());
        String authNonce = UUID.randomUUID().toString();

        StringBuilder authStringBuilder = new StringBuilder();
        authStringBuilder.append(API_TOKEN).append("&");
        authStringBuilder.append(timestamp).append("&");
        authStringBuilder.append(authNonce).append("&");
        authStringBuilder.append("GET").append("&");
        authStringBuilder.append(path);

        String authString = authStringBuilder.toString();
        Mac mac = Mac.getInstance("HmacSHA256");
        SecretKeySpec secretKey = new SecretKeySpec(API_SECRET.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
        mac.init(secretKey);

        byte[] hmacBytes = mac.doFinal(authString.getBytes(StandardCharsets.UTF_8));
        String authSignature = Base64.getEncoder().encodeToString(hmacBytes);

        HttpHeaders headers = new HttpHeaders();
        headers.set("Auth-Token", API_TOKEN);
        headers.set("Auth-Nonce", authNonce);
        headers.set("Auth-Timestamp", timestamp);
        headers.set("Auth-Signature", authSignature);
        headers.set("Content-Type", "application/json");

        RestTemplate restTemplate = new RestTemplate();
        HttpEntity<String> request = new HttpEntity<>(headers);
        ResponseEntity<String> response = restTemplate.exchange(new URI(url), HttpMethod.GET, request, String.class);
return response.getBody();

```

---

<div class="post-metadata">

### Author: ![zach](https://forum.pritunl.com/user_avatar/forum.pritunl.com/zach/32/1105_2.png) [@zach](https://forum.pritunl.com/u/zach)
#### Post date: [January 26, 2025, 6:05pm UTC](https://forum.pritunl.com/t/pritunl-rest-api-authentication-using-java/2999/2 "2025-01-26T18:05:15Z")

</div>

The [**auditing journal**](https://docs.pritunl.com/docs/auditing#journal-audit) can be enabled by setting Auditing Mode to All in the top right settings. This will then log the exact cause of the 401 error to `/var/log/pritunl_journal.log`.

The nonce is incorrect in that code it should be `UUID.randomUUID().toString().replace("-", "")` to remove the dashes.
