# Auth0 Java SDK - Refreshing Management API Token

**URL:** <https://community.auth0.com/t/auth0-java-sdk-refreshing-management-api-token/93857>\
**Category:** Get Help\
**Tags:** sdk:java, management-api, auth0\
**Created:** [November 7, 2022, 4:05pm UTC](https://community.auth0.com/t/auth0-java-sdk-refreshing-management-api-token/93857 "2022-11-07T16:05:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![krisnik](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/krisnik/32/40834_2.png) [@krisnik](https://community.auth0.com/u/krisnik)\
**Post date:** [November 7, 2022, 4:05pm UTC](https://community.auth0.com/t/auth0-java-sdk-refreshing-management-api-token/93857/1 "2022-11-07T16:05:16Z")

</div>

Is there a recommended way of handling Management API token refresh?

In my mind there are three ways…

1. Handle the API exception, refresh token and re-run the request.
2. Store `tokenExpiresAt` and refresh the token when `managementAPI` instance is required. Something in the lines of

```java
public ManagementAPI getManagementAPI() {

    if (currentTime > tokenExpiresAt) {
        refreshToken();
        managementApi.setApiToken("new token");
    }

    return managementApi;
}

```

1. Have a background thread that refreshes the token at defined periodicity.

All three options have some level of drawbacks.

For 1, retries should be handled at each Auth0 call. the code becomes repetitive.  
In 2, we cannot let callers cache managementAPI object. They should always use `getManagementAPI()`.  
In case of 3, additional handling is required to ensure that the background thread is re-created if it crashes for any reason.

Please share your suggestions and thoughts.

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [November 8, 2022, 5:01pm UTC](https://community.auth0.com/t/auth0-java-sdk-refreshing-management-api-token/93857/3 "2022-11-08T17:01:52Z")

</div>

Hi @krisnik,

Welcome to the Auth0 Community!

I’m a bit surprised the library doesn’t handle this automatically. It may be worth submitting an [issue](https://github.com/auth0/auth0-java/issues) in the GH repo and see if this is a bug.

For now, I would suggest either inspecting the `exp` ([Expiration Time](https://www.rfc-editor.org/rfc/rfc7519#section-4.1.4)) claim, or refreshing when you receive the error.

---

<div class="post-metadata">

**Author:** ![krisnik](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/krisnik/32/40834_2.png) [@krisnik](https://community.auth0.com/u/krisnik)\
**Post date:** [November 16, 2022, 9:50am UTC](https://community.auth0.com/t/auth0-java-sdk-refreshing-management-api-token/93857/4 "2022-11-16T09:50:41Z")

</div>

Thanks for the response. Have created - [Auto-refresh of API Token for Management APIs · Issue #473 · auth0/auth0-java · GitHub](https://github.com/auth0/auth0-java/issues/473)

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [November 16, 2022, 4:26pm UTC](https://community.auth0.com/t/auth0-java-sdk-refreshing-management-api-token/93857/5 "2022-11-16T16:26:27Z")

</div>

Thank you! Let us know if you have any other questions.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [November 30, 2022, 4:26pm UTC](https://community.auth0.com/t/auth0-java-sdk-refreshing-management-api-token/93857/6 "2022-11-30T16:26:42Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
