401 Unauthorized when making an API request

Hello, I am new to the Atlassian cloud dev just trying to make some macros for my first internship at a company.

I have the following index.js file and all I’m trying to do is display the title of a specific page in our confluence cloud.

import Resolver from '@forge/resolver';
import api, {route} from '@forge/api';

const fetchPageInfo = async () => {
  console.log("Fetch Page Info Called Now.");
  const response = await api.asUser().requestConfluence(route`/wiki/api/v2/pages/1121779764`, {
    headers: {
      'Accept': 'application/json'
    }
  });
  
  console.log(`Response: ${response.status} ${response.statusText}`);
  console.log(await response.json());
}

const resolver = new Resolver();

resolver.define('getText', (req) => {
  console.log(req);
  fetchPageInfo();
  return "Hello World";
});

export const handler = resolver.getDefinitions();

I am getting 401 unauthorized when I make the request. I have tried forge lint --fix and it does not work no matter what permissions it adds to the manifest I can’t seem to get any data back from the API req.

Any help would be appreciated,
Thanks :slight_smile:

Welcome to the Atlassian Developer Community, @JustAnotherIntern!

I tried out your code, replacing the pageId in the REST API call, and adding this scope in the manifest.yml file, and so far, it is working as expected.

permissions:
  scopes:
    - read:page:confluence

After updating your manifest file to add the necessary scope, did you run a forge deploy followed by a forge install --upgrade so that the manifest changes would take effect?

Also, can you share the permissions you added?

Cheers,
Ian

2 Likes

Hey Ian thank you so much for getting back to me quickly.

You’re permissions work for me and I’m up and going again so thank you!

I was referencing the following link when building and was wondering if there was something else I should be looking at like where you found that permission?
https://developer.atlassian.com/platform/forge/manifest-reference/scopes-product-confluence/

Once again thank you!

1 Like

Hi @JustAnotherIntern,

Based on the code snippet in your original post, you are using the Get page by ID REST API. To check the necessary scope for this API, you need to visit the developer docs: this one is for the said REST API.

You can see the necessary Forge scope in the highlighted section in the image below.

Hope this helps!

1 Like