71
Views
8
Comments
Solved
rest API access token expired

Hi all,

          I have a rest API, The access token will expire in one hour. How can i do ... Please provide some reference examples. Thank you !!

Api.oml
2021-11-12 04-59-31
Manikandan Sambasivam
Solution
AI Generated

To handle an expiring access token in OutSystems, you can implement a mechanism to refresh the token before it expires. 

  1. Store the Access Token and Expiry Time:

    • Use site properties or session variables to store the access token and its expiry time.
  2. Create a Token Refresh Logic:

    • Create a server action to check if the token is expired and refresh it if necessary.
  3. Integrate the Token Refresh Logic with API Calls:

    • Ensure that every API call checks the token status and refreshes it if needed before making the request.

Implementation : 

Step 1: Store the Access Token and Expiry Time

  1. Create Site Properties:
    • AccessToken of type Text.
    • TokenExpiryTime of type DateTime.

Step 2: Create a Token Refresh Logic

  1. Create a Server Action to Refresh the Token:
    • Create a new server action (e.g., RefreshAccessToken).
    • In this action, make a request to the authentication endpoint to get a new token.

Step 3: Integrate the Token Refresh Logic with API Calls

  1. Create a Server Action to Check and Refresh Token:
    • Create a new server action (e.g., CheckAndRefreshToken).
    • This action will check if the token is expired and call RefreshAccessToken if necessary.
  2. Use the CheckAndRefreshToken Action in Your API Calls:
    • Before making any API request, call the CheckAndRefreshToken action to ensure you have a valid token.
This answer was AI-generated. Please read it carefully and use the forums for clarifications
UserImage.jpg
NIZHEN LIN

1. Store the Access Token and Expiry Time in entity (Token)

2.  Create a Server Action to Refresh the Token (RefreshAcessToken)

3. check if the token is expired and call RefreshAccessToken (CheckAndRefreshToken)


But its still not running... I share my oml. Can you help me find it ... Thank you !!

Token.oml
2024-05-22 06-12-56
Vignesh Prakash

Hi @NIZHEN LIN

I believe the OML is fine and just need a condition to be updated in the server action.

You have written a condition that checks 

CurrDateTime() < GetTokens.List.Current.Token.TokenExpiryAt which should be the other way around. 

As per the condition written the flow will move to false all the time and only if the current time is lesser than the expiry you are refreshing the token. 

eg: lets assume the current date and time is 6/4/2024  3:49:06 PM and the token expiry is 6/4/2024  2:49:06 PM

As per the existing condition  6/4/2024  3:49:06 PM < 6/4/2024  2:49:06 PM which is false and token will not be refreshed. 

You can either swap the if condition connectors or update the condition as CurrDateTime() > GetTokens.List.Current.Token.TokenExpiryAt 

Please update the if condition on the refresh server action and it should work fine.

Thanks,
VP.

UserImage.jpg
NIZHEN LIN

Thank you!! Refresh Token working :)

I still have a error ... My Access Token use on "Authorization" not running...

Can you help me find it Thank you !!

GetToken_1.jpg
GetToken_2.jpg
2023-08-18 10-40-36
Deepak M

I didn't understand what you were exactly asking for

If it is about exposing API: you need to save the token generated time along with the token and need to check by logically whether it is used within an hour or not.

Consuming API: You need to find the refresh token API from the respected documentation site, and you can call that to avoid the expiration.

UserImage.jpg
NIZHEN LIN

I use Consuming Api and refresh token. But Its still not running... I share my oml. Can you help me find it... Thank you !!

Token.oml
UserImage.jpg
NIZHEN LIN

I refer the below links and it still not running ... what can I do ... Thank you !!

Token.oml
2021-11-12 04-59-31
Manikandan Sambasivam
Solution
AI Generated

To handle an expiring access token in OutSystems, you can implement a mechanism to refresh the token before it expires. 

  1. Store the Access Token and Expiry Time:

    • Use site properties or session variables to store the access token and its expiry time.
  2. Create a Token Refresh Logic:

    • Create a server action to check if the token is expired and refresh it if necessary.
  3. Integrate the Token Refresh Logic with API Calls:

    • Ensure that every API call checks the token status and refreshes it if needed before making the request.

Implementation : 

Step 1: Store the Access Token and Expiry Time

  1. Create Site Properties:
    • AccessToken of type Text.
    • TokenExpiryTime of type DateTime.

Step 2: Create a Token Refresh Logic

  1. Create a Server Action to Refresh the Token:
    • Create a new server action (e.g., RefreshAccessToken).
    • In this action, make a request to the authentication endpoint to get a new token.

Step 3: Integrate the Token Refresh Logic with API Calls

  1. Create a Server Action to Check and Refresh Token:
    • Create a new server action (e.g., CheckAndRefreshToken).
    • This action will check if the token is expired and call RefreshAccessToken if necessary.
  2. Use the CheckAndRefreshToken Action in Your API Calls:
    • Before making any API request, call the CheckAndRefreshToken action to ensure you have a valid token.
This answer was AI-generated. Please read it carefully and use the forums for clarifications
UserImage.jpg
NIZHEN LIN

1. Store the Access Token and Expiry Time in entity (Token)

2.  Create a Server Action to Refresh the Token (RefreshAcessToken)

3. check if the token is expired and call RefreshAccessToken (CheckAndRefreshToken)


But its still not running... I share my oml. Can you help me find it ... Thank you !!

Token.oml
2024-05-22 06-12-56
Vignesh Prakash

Hi @NIZHEN LIN

I believe the OML is fine and just need a condition to be updated in the server action.

You have written a condition that checks 

CurrDateTime() < GetTokens.List.Current.Token.TokenExpiryAt which should be the other way around. 

As per the condition written the flow will move to false all the time and only if the current time is lesser than the expiry you are refreshing the token. 

eg: lets assume the current date and time is 6/4/2024  3:49:06 PM and the token expiry is 6/4/2024  2:49:06 PM

As per the existing condition  6/4/2024  3:49:06 PM < 6/4/2024  2:49:06 PM which is false and token will not be refreshed. 

You can either swap the if condition connectors or update the condition as CurrDateTime() > GetTokens.List.Current.Token.TokenExpiryAt 

Please update the if condition on the refresh server action and it should work fine.

Thanks,
VP.

UserImage.jpg
NIZHEN LIN

Thank you!! Refresh Token working :)

I still have a error ... My Access Token use on "Authorization" not running...

Can you help me find it Thank you !!

GetToken_1.jpg
GetToken_2.jpg
Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.