If you are unable to create a new account, please email support@bspsoftware.com

 

Leveraging the Cognos Analytics REST API from a JavaScript extension

Started by dougp, 30 Oct 2024 01:51:14 PM

Previous topic - Next topic

dougp

I have been trying to leverage the Cognos Analytics REST API from PowerShell and have had some success.  But now I'd like to use it in the context of Cognos Analytics as a JavaScript-based extension.

If I'm logging in directly through the dispatcher (https://servername:9300/), I can run this script to return a list of data modules.

// get base URI from Cognos
const origin = document.location.origin;


// Use this for connecting directly to the dispatcher
//const port = "9300";
const uriBase = origin; //+ ":" + port;

//  // Use this for SSO
//  const port = "9300";
//  const uriBase = origin + ":" + port;


// URI to retrieve all data modules
const uri = uriBase + "/api/v1/modules";

// set the request method
const method = 'GET';

// set the content type
const contentType = "application/json; charset=utf-8";

// inspect the site's cookies to get the XSRF token
const a = document.cookie.split(";");
let b = "";
a.forEach((e) => { if (e.startsWith("XSRF-TOKEN=")) b = e;});
const XSRFTOKEN = b.replace("XSRF-TOKEN=", "");

// set the headers and options for the fetch
const headers = {
'x-xsrf-token': XSRFTOKEN,
accept: 'application/json',
'Cache-Control': 'no-cache',
'Content-Type': contentType
}

const requestOptions = {
method: method,
headers: headers
}

// finally, make the call
let output = await fetch (uri, requestOptions)
.then(response => {
if (!response.ok) {
  throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
return data;
})
.catch(error => {
console.error('Error:', error);
});

console.log(JSON.stringify(output, null, 2));

But if I log in using single sign-on through IIS (https://servername/ibmcognos), and slightly modify my code (see inline comments), it doesn't work.

Access to fetch at 'https://servername:9300/api/v1/modules' from origin 'https://servername' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
Thinking that the Cognos Analytics Firewall may help, I added servername and servername:9300 to the list.  No love.

I have also tried setting either header.mode = 'no-cors' or requestOptions.mode = 'no-cors'.

What changes are needed to use the Cognos Analytics REST API while using single sign-on on IIS?


MFGF

Quote from: dougp on 30 Oct 2024 01:51:14 PMI have been trying to leverage the Cognos Analytics REST API from PowerShell and have had some success.  But now I'd like to use it in the context of Cognos Analytics as a JavaScript-based extension.

If I'm logging in directly through the dispatcher (https://servername:9300/), I can run this script to return a list of data modules.

// get base URI from Cognos
const origin = document.location.origin;


// Use this for connecting directly to the dispatcher
//const port = "9300";
const uriBase = origin; //+ ":" + port;

//  // Use this for SSO
//  const port = "9300";
//  const uriBase = origin + ":" + port;


// URI to retrieve all data modules
const uri = uriBase + "/api/v1/modules";

// set the request method
const method = 'GET';

// set the content type
const contentType = "application/json; charset=utf-8";

// inspect the site's cookies to get the XSRF token
const a = document.cookie.split(";");
let b = "";
a.forEach((e) => { if (e.startsWith("XSRF-TOKEN=")) b = e;});
const XSRFTOKEN = b.replace("XSRF-TOKEN=", "");

// set the headers and options for the fetch
const headers = {
'x-xsrf-token': XSRFTOKEN,
accept: 'application/json',
'Cache-Control': 'no-cache',
'Content-Type': contentType
}

const requestOptions = {
method: method,
headers: headers
}

// finally, make the call
let output = await fetch (uri, requestOptions)
.then(response => {
if (!response.ok) {
  throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
return data;
})
.catch(error => {
console.error('Error:', error);
});

console.log(JSON.stringify(output, null, 2));

But if I log in using single sign-on through IIS (https://servername/ibmcognos), and slightly modify my code (see inline comments), it doesn't work.

Access to fetch at 'https://servername:9300/api/v1/modules' from origin 'https://servername' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
Thinking that the Cognos Analytics Firewall may help, I added servername and servername:9300 to the list.  No love.

I have also tried setting either header.mode = 'no-cors' or requestOptions.mode = 'no-cors'.

What changes are needed to use the Cognos Analytics REST API while using single sign-on on IIS?



I'm a long way from being an IIS expert, but my suspicion here is that you need to set up an origin host rule in IIS to support your host domain. Probably the easiest way is to set a * origin host rule.

There is some good info in the IIS documentation here:

https://learn.microsoft.com/en-us/iis/extensions/cors-module/cors-module-configuration-reference

I don't use IIS, so it's not something I can test, unfortunately.

Cheers!

MF.
Meep!