Avoid invoking application microservices with expired token

Hi, everybody.

We are developing an application with several microservices. Invoking these microservices can include multiple calls to the Genesys API. We use client credentials grant authentication and set a exipry time of 300 seconds. I read in this post How to check token expiry? - #3 by tim.smith

"Also keep in mind that a well-formed application will never rely on the stated expiry time and will be able to gracefully handle a token expiring at any time for any reason."

We have implemented a strategy that when a com.mypurecloud.sdk.v2.ApiException occurs we catch it and if apiException.getStatusCode() == 401 we create a new instance of com.mypurecloud.sdk.v2.ApiClient by calling the setDefaultApiClient() method which is below:

public void setDefaultApiClient() {

ApiClient.Builder builder = ...
builder.withConnectionTimeout(...
builder.withBasePath(...
...
ApiClient apiClient = builder.build();
ApiResponse authResponse = apiClient.authorizeClientCredentials(clientId, clientSecret);
Configuration.setDefaultApiClient(apiClient);

}

Apart from the strategy explained in the previous lines to renew the token in case of a 401 error, and in order to make sure that we are never going to invoke an our application microservice with an expired token... can be considered a good practice to call the setDefaultApiClient() method every time an our application microservice is invoked? This could happen several times a second... or every 2 minutes... or every 10 minutes...

Thanks.

I would recommend obtaining a new token only when you know the previous one has expired (401 response, or you know the current time is after the expiry time before making the request, or you don't have any token). There shouldn't be any need to rebuild everything; just call authorizeClientCredentials on the existing ApiClient object. Or if you want to get a token on your own, do that then call setAccessToken on the existing ApiClient.

Ultimately, you're free to use the SDK however you want if it works for you and your application doesn't make abusive requests to the platform.

We will do it.

Thank you for your answer.

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