Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Qlik and ServiceNow Partner to Bring Trusted Enterprise Context into AI-Powered Workflows. Learn More!
cancel
Showing results for 
Search instead for 
Did you mean: 
AN2024
Contributor II
Contributor II

Renew Token - Rest Connection

I am using a REST Connection and the token expires each hour, therefore I am using sub function to create a subrutine for the token table and after I use the call function to bring the subrutine, nevertheless the token doesn´t renew and 1 hour later I have the error in code because of expire time.

 

I appreciate if someone could help with this issue.

 

Below you will see the code

 

Use of sub function:

SUB ActualizarToken
 
LIB CONNECT TO 'Conexion';
 
RestConnectorMasterTable:
SQL SELECT 
"access_token",
"scope",
"api_domain",
"token_type",
"expires_in"
FROM JSON (wrap on) "root");
 
[root]:
LOAD [access_token],
[scope],
[api_domain],
[token_type],
[expires_in]
RESIDENT RestConnectorMasterTable;
 
LET vAccessToken = Peek('access_token', 0, 'root');
LET vConcatenated_token = 'Zoho-oauthtoken ' & vAccessToken;
 
 LET vTokenExpiresAt = Now() + (Peek('expires_in', 0, 'root') / 86400);
 
 
LET vTokenExpiresAtFormatted = Timestamp($(vTokenExpiresAt));
 
 
TRACE $(vConcatenated_token);
 
TRACE $(vTokenExpiresAt);
 
TRACE Expira a las: $(vTokenExpiresAtFormatted);
 
 
DROP TABLE RestConnectorMasterTable;
 
 
END SUB




I use the Call Function:

 

 

Call ActualizarToken;
 
LET rows_per_page = 100;
let vmaxPages=60;
for vPage = 1 to $(vmaxPages)
 
    
IF Now() > $(vTokenExpiresAt) THEN
        Call ActualizarToken;  
    ENDIF
Labels (2)
1 Reply
Chanty4u
MVP
MVP

Replace with your current expiry policy with below code and give a try 

// Calculate absolute expiry time

LET vTokenExpiresAt = Now() + (Peek('expires_in', 0, 'root') / 86400);

 

// Force renewal 2 minutes before expiry

LET vRenewBefore = Timestamp(Now() + (2/1440)); // 2 minutes

 

IF $(vTokenExpiresAt) <= $(vRenewBefore) THEN

    TRACE Token expired → refreshing again...

    CALL ActualizarToken;

END IF;