OAuth2 machine credential using JavaScript
// m2m-oauth-example.js uses the Qlik Platform SDK with node.js to make an
// authenticated connection to Qlik as a non-interactive user.
// To use this example, create an OAuth2 web configuration in Qlik Cloud and set
// the Allow Machine-to-Machine option.
// Copy the clientId and clientSecret to a secure location, then set the consent
// to trusted.
// This example is written for Node.js using common.js for module use.
// Note if using < Node.js 18.x
// install node-fetch package and
// use the code below to add node-fetch to the global.fetch of the code.
// global.fetch = (...args) => import('node-fetch')
// .then(({default: fetch}) => fetch(...args));
import { Auth, AuthType, Apps } from '@qlik/sdk';
const auth = new Auth({
authType: AuthType.OAuth2,
host: "<tenant-hostname.region.qlikcloud.com>",
clientId: "<client id from OAuth2 configuration in Qlik Cloud>",
clientSecret: "<client secret from OAuth2 configuration in Qlik Cloud>"
});
(async () => {
await auth.authorize();
let resp = await auth.rest('/users/me').then(output => output.json());
console.log(resp);
// get a list of applications
resp = await auth.rest('/apps').then(output => output.json());
// iterate through the resulting array and retrieve the app id and name
resp.data.forEach((item) => {
// console output to show the app information from the rest endpoint
console.log(`${item.attributes.id} - ${item.attributes.name}`);
});
const apps = new Apps(auth);
// get the reference to the first app returned in the list
let app = await apps.get(resp.data[0].attributes.id);
// open the app
await app.open();
// get the app layout
let appLayout = await app.getAppLayout();
// console output from the Qlik Analytics Engine containing app metadata
console.log(appLayout);
});
Check it out on Replit.
Read the tutorial