Trying to write a custom code to fetch the knowledge base articles from our JSM

Trrying to write a custom code to fetch the knowledge base articles from our JSM . We tried using details at https://developer.atlassian.com/cloud/jira/service-desk/rest/api-group-knowledgebase/#api-rest-servicedeskapi-knowledgebase-article-get but it is not working for us . WE keep on getting {
“error”: “Failed to parse Connect Session Auth Token”
}

Hi @kompalsithta. Welcome to the developer community.

How are you calling the API? (ex: from a Forge app, a Connect app, or maybe just from a REST client like Postman)

Hi @nmansilla ,

I am using the REST call and using python for the same.

This is my code:

import requests
import json

url = "https://id.atlassian.net/rest/servicedeskapi/knowledgebase/article?query='test'"


headers = {
  "X-ExperimentalApi": "opt-in",
  "Accept": "application/json",
  "Authorization": "Bearer XXXXXXXXX"
}

response = requests.request(
   "GET",
   url,
   headers=headers
)

print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))

@kompalsithta,

Unfortunately, the REST API reference docs “lie” about how to authenticate. To be precise, the docs do not adequately represent the multiple ways which code might need to authenticate. If you are using Python, I assume you’ll want to pair that with an API token and use Basic authentication.

For Python requests, the following snippet replaces the current headers and response statements:

headers = {
  "X-ExperimentalApi": "opt-in",
  "Accept": "application/json",
}
basic = HTTPBasicAuth(USER_EMAIL, USER_API_TOKEN)
response = requests.request(
   "GET",
   url,
   headers=headers,
   auth=basic,
)

Note: The Bearer header is not correct for this method of auth; hence, removed. And the USER_EMAIL and USER_API_TOKEN variables must be initialized in prior statements.

1 Like

How do we do this from a Forge UI kit app?

I’ve tried every method I can think of.

  const fetchKb = async () => {
    const data = await (
      await requestJira(`/rest/servicedeskapi/knowledgebase/article?query=IT%20Support`)
    ).json();
    console.log("data", data);
    let url = data.values[0].content.iframeSrc;
    let index = url.indexOf("/rest/service");
    url = url.slice(index);
    let know = await requestConfluence(url);
    console.log("know", await know.text());
    let know2 = await requestJira(url);
    console.log("know2", await know2.text());
    let know3 = await requestConfluence(
      `/wiki/rest/api/content/${data.values[0].source.pageId}?expand=body.storage`
    );
    console.log("know3", await know3.text());
    let know4 = await requestJira(
      `/wiki/rest/api/content/${data.values[0].source.pageId}?expand=body.storage`
    );
    console.log("know4", await know4.text());

All are responding 401 Unauthorized; scope does not match my manifests scopes are

    - read:jira-work
    - write:jira-work
    - read:knowledgebase:jira-service-management

But i suspect there is some other secret scope I need.

Hello @Christopher ! You are trying to execute this request from the frontend. That is why I added this code to my frontend component

import { requestJira } from "@forge/bridge";
useEffect(() => {
    (async () => {
      const response = await requestJira(
        "/rest/servicedeskapi/knowledgebase/article?query=IT%20Support"
      );
      console.log(await response.text());
    })();
  }, []);

I have the following scopes:

scopes:
- read:jira-work
- read:knowledgebase:jira-service-management

And I was able to execute this code successfully.

Also make sure that you executed forge install --upgrade after you added your scopes

Thank you!. I am trying to get the content as well. As frankly, getting whatever is returned from /rest/servicedeskapi/knowledgebase/article?query=whatever is pretty useless.

I was able to get this to work. The secret scope was any one of, or both.

    - read:confluence-content.summary
    - read:custom-content:confluence

and it was know3 that worked.

so tying it all together:

    const data = await (
      await requestJira(`/rest/servicedeskapi/knowledgebase/article?query=IT%20Support`)
    ).json();
    let know = await (
      await requestConfluence(
        `/wiki/rest/api/content/${data.values[0].source.pageId}?expand=body.storage`
      )
    ).json();
    setKb(know.body.storage.value);