Most APIs make you log in again after some time. You get a token, but it stops working after a while. If you run your task for a long time, it will give you an error because the token is no longer valid.
When your token runs out, check the time before you make a call. If needed, get a new token. I tested this using a free API in Power Automate.
Without going into more detail, let’s dive into the Power Automate solution and demo it.
For this demo I used dummyJSON, which you can access from this link (https://dummyjson.com/) and it doesn’t need sign-up.
Let’s build the flow now:
The image below depicts the trigger and three initialize variable actions, there are all of type string and the names are: accessToken, refreshToken, and tokenExpiry.

As shown in the image below, the next step is the HTTP action, where I’ll share more details to help you.

Set it to POST “https://dummyjson.com/auth/login“, with the header Content-Type: application/json and this body:
You can find the login details on the following page: https://dummyjson.com/docs/users.
{
“username”: “emilys”,
“password”: “emilyspass”,
“expiresInMins”: 1
}

In the next three steps, I set variables to save the results from the HTTP action.
| Action | Value |
| Set variable: accessToken | body(‘Get_Token’)?[‘accessToken’] |
| Set variable: refreshToken | body(‘Get_Token’)?[‘refreshToken’] |
| Set variable: tokenExpiry | addSeconds(utcNow(), 60) |
I’ve added the Delay action so the token has enough time to expire.
The final image contains the actions that need to run after the token expires.

The condition is using the expression below to check if the token is expired
“greaterOrEquals(ticks(utcNow()), ticks(variables(‘tokenExpiry’)))”
The above expression asks whether the time has passed the deadline.
If the condition returns Yes then it runs the HTTP actions that produces the Refresh Token
Set it to POST “https://dummyjson.com/auth/login“, with the header Content-Type: application/json and this body:
{
“refreshToken”: “@{variables(‘refreshToken’)}“,
“expiresInMins”: 1
}

Then it sets again the three variables with the new values, and the last action is an HTTP that returns the test body depicted below:
After the condition, make the request. Use the Authorization header with the access token. Then, send a POST request to https://dummyjson.com/auth/products/add/
Include a simple message for Body like {“title”: “Test from Power Automate Flow”}.

After running the flow, and after one minute has passed, you will get an output like the one below:


