COGNOiSe.com - The IBM Cognos Community

IBM Cognos Analytics Platform => Cognos Analytics => Administration and Security => Topic started by: dougp on 19 Aug 2021 01:45:26 PM

Title: Cognos 11.1 REST API - working example
Post by: dougp on 19 Aug 2021 01:45:26 PM
Does anyone here have a simple, working example of how to use the REST API that's in Cognos Analytics 11.1.7?


If I can't get simple things working through the "Try it out" feature, it seems unlikely I'll get some working code built.  And the documentation is pretty useless.

Following the documentation (https://www.ibm.com/docs/en/cognos-analytics/11.1.0?topic=apis-rest-api) I go to https://<cognos_analytics_server>:<port>/api/api-docs.  From there I go to Session | Get | Try it out | Execute and I get a response that is just a bunch of codes that are not human-readable.  Then I tried Session | Put | Try it out and entered the appropriate JSON string...

{
  "parameters": [
    {
      "name": "CAMNamespace",
      "value": "LDAP"
    },
    {
      "name": "CAMUsername",
      "value": "johnsmith"
    },
    {
      "name": "CAMPassword",
      "value": "mypassword"
    }
  ]
}


...replacing LDAP with the name of my directory namespace and the values for username and password to mine.  I get this error message:

Quote403  Error:  Forbidden
{
  "errorCodeString": "camAuthUnrecoverable",
  "messages": [
    {
      "messageString": "AAA-AUT-0011 Invalid namespace was selected."
    }
  ],
  "promptInfo": {
    "captions": [
      "Invalid namespace was selected."
    ]
  }
}
Title: Re: Cognos 11.1 REST API - working example
Post by: CognosPaul on 20 Aug 2021 02:42:19 AM
It looks like it doesn't like the namespace ID you're using.

From an authenticated session in Cognos do this:


var
    getCookie     = function(name) {
      const value   = `; ${document.cookie}`;
      const parts   = value.split(`; ${name}=`);
      if (parts.length === 2) return parts.pop().split(';').shift();
    }
   , gateway       = __glassAppController.glassContext.gateway
   , auth          = 'CAM ' + getCookie('cam_passport')
   , xsrf          = getCookie('XSRF-TOKEN')
   , getData       = {
        method: 'get'
      , credentials: 'include'
      , headers: {
           'Content-Type': 'application/json'
          , 'IBM-BA-Authorization': auth
          , 'x-xsrf-token' : xsrf
         }
       }

  , objs          = await fetch(gateway+"/v1/namespaces",getData)
  , namespaces         = await objs.json()


Then take a look at the namespaces object. Pull the correct namespace name from there. I'm not sure if it wants the ID, the Default Name, or the Search Path. The instance I have access to right now has some funky redirects making testing PUT very difficult.

Title: Re: Cognos 11.1 REST API - working example
Post by: dougp on 20 Aug 2021 03:13:22 PM
Thanks, Paul.
It looks like I had the correct namespace name, but the results are inconsistent.  I tried id and defaultName several times each (because if you do the same thing over and over and expect different results, you're probably using modern software).  One of the attempts when using the defaultName succeeded.  I am currently unable to reproduce that success.
Title: Re: Cognos 11.1 REST API - working example
Post by: CognosPaul on 23 Aug 2021 03:21:26 AM
Are you testing in incognito mode? Is SSO enabled?
Title: Re: Cognos 11.1 REST API - working example
Post by: dougp on 23 Aug 2021 10:32:29 AM
Perfect.  Thanks for that.  Considering incognito mode forced me to think about this differently.

It looks like I could not get a session because there was already an active session in that browser, because I had Cognos open.

Getting a session works (response 201) if there is not one.
Getting a session fails (response 403) if a session already exists.
I don't know how I would update the session (response 200).  Does this really mean creating a new session as a different user?  But that would not be an update because it would require a different session id.
In the docs, I can click Session | Delete to be sure I don't have a session.

But none of this shows me how to do this in JavaScript.  For example, the URL to create a session and to delete a session appear to be the same.  There are no instructions regarding what else I would pass to that URL to delete the session.  The documentation appears to me to be incomplete.  So, back to my original question:

QuoteDoes anyone here have a simple, working example of how to use the REST API that's in Cognos Analytics 11.1.7?

Then, let me go a step further:
Does IBM provide more complete documentation than what I see in the link in my original post?
Is there a way to query the API to get it to tell me what it needs?


Title: Re: Cognos 11.1 REST API - working example
Post by: CognosPaul on 24 Aug 2021 06:22:05 AM
The URL would be the same for creating and closing sessions, it's just the request options that are different. The documentation could definitely use a lot of work. The get/post/delete are referring to the method in the request. When doing a post or a delete you will usually need to include a body, but that would a be stringified object.

createSession       = {
    method: 'put'
  , credentials: 'include'
  , headers: {
       'Content-Type': 'application/json'
  }
  , body: '{"parameters": [{"name": "CAMNamespace","value": "LDAP"},{"name": "CAMUsername","value": "johnsmith"},{"name": "CAMPassword","value": "mypassword"}]}'
}

fetch(gateway+"/api/v1/session",createSession)


For the delete you can just use this object:

deleteSession       = {
    method: 'delete'
  , credentials: 'include'
  , headers: {
       'Content-Type': 'application/json'
      , 'IBM-BA-Authorization': auth
      , 'x-xsrf-token' : xsrf
  }
}


Then the fetch would look like:
fetch(gateway+"/api/v1/session",deleteSession)


Unfortunately I haven't found another source, the documentation you see is what they have. I'd be very happy to help figure things out though.
Title: Re: Cognos 11.1 REST API - working example
Post by: raztos on 17 Feb 2023 02:40:39 AM
Hello dougp,

forget about debugging Javascript. That's not the point why doesn't work. Just a few questions. Is your Cognos 11.1.7 on Cloud Hosted, Dedicated?
Title: Re: Cognos 11.1 REST API - working example
Post by: dougp on 17 Feb 2023 02:18:00 PM
on prem
Title: Re: Cognos 11.1 REST API - working example
Post by: macsir on 12 Mar 2023 08:49:41 PM
Quote from: dougp on 17 Feb 2023 02:18:00 PM
on prem

Hi, have you got it working yet? I even can't get a session by using the suggested format from IBM.
https://www.ibm.com/docs/en/cognos-analytics/11.1.0?topic=apis-rest-api