Skip to content

Instantly share code, notes, and snippets.

@hurrifan1
Created October 4, 2021 17:46
Show Gist options
  • Save hurrifan1/2155fac28269af543215d60b22153c54 to your computer and use it in GitHub Desktop.
Save hurrifan1/2155fac28269af543215d60b22153c54 to your computer and use it in GitHub Desktop.
APIs in Qlik Sense
/* Working with API tokens in Qlik script */
// =====================================================================================================
// GET NEW TOKEN
// =====================================================================================================
SUB GetGraphToken
LIB CONNECT TO 'Azure - Microsoft Graph API - AUTH';
RestConnectorMasterTable:
SQL SELECT
"token_type",
"expires_in",
"ext_expires_in",
"access_token"
FROM JSON (wrap on) "root";
[GraphToken]:
LOAD
Now() as [token_acquired_ts],
[token_type],
[expires_in],
[ext_expires_in],
[access_token]
RESIDENT RestConnectorMasterTable;
DROP TABLE RestConnectorMasterTable;
Store [GraphToken] into 'lib://SomeFolder/GraphToken.qvd'(QVD);
Let vToken = peek('access_token', 0, 'GraphToken');
Drop Table GraphToken;
END SUB
// =====================================================================================================
// TOKEN CHECK
// =====================================================================================================
SUB TokenCheck
Let vNow = Now();
Let vQVD_ts = Coalesce(timestamp#( QvdCreateTime('lib://SomeFolder/GraphToken.qvd') + (60/60/24) - ((60/60/24/60/60)*5) ), 0);
Let vCompare = timestamp('$(vNow)') > timestamp('$(vQVD_ts)');
// Trace >>>>>>>>> vNow = $(vNow);
// Trace >>>>>>>>> vQVD_ts = $(vQVD_ts);
// Trace >>>>>>>>> vCompare = $(vCompare);
IF timestamp(Now()) > timestamp(Coalesce( QvdCreateTime('lib://SomeFolder/GraphToken.qvd') + (60/60/24) - ((60/60/24/60/60)*5), 0) ) THEN
Trace #### Graph token expired...getting new token now.;
CALL GetGraphToken
ELSE
Trace #### Graph token still good.;
GraphToken: LOAD [access_token] FROM 'lib://SomeFolder/GraphToken.qvd'(QVD);
Let vToken = peek('access_token', 0, 'GraphToken');
// Trace > > > > > > TOKEN:;
// Trace $(vToken);
Drop Table GraphToken;
END IF
END SUB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment