Error 401 when calling Confluence REST API from dynamicProperties function

Hey guys,

I’ve created a blank Forge UI Kit 2 app with a module confluence:contentBylineItem:

modules:
  confluence:contentBylineItem:
    - key: content-page-byline
      title: Content check
      resource: pageByline
      render: native
      dynamicProperties:
        function: pageBylineInit
  function:
    - key: pageBylineInit
      handler: index.pageBylineInit
resources:
  - key: pageByline
    path: src/pageByline/index.jsx

I’m trying to call Confluence REST API from a dynamicProperties function:

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

export const pageBylineInit = async (req) => {
  console.log(req);
  const result = await api
    .asUser()
    .requestConfluence(route`/wiki/api/v2/pages/5879627782`, {
      headers: {
        'Accept': 'application/json'
      }
    });
  const data = await result.json();
  console.log(data);
  return { title: 'Hello, world!' };
}

I am getting the following error:

ERROR   15:17:58.727  3d639a37-ffcd-4e22-9977-7164629fffa3  [NEEDS_AUTHENTICATION_ERR: Authentication Required] {
  serviceKey: 'atlassian-token-service-key',
  options: undefined,
  status: 401
}

I double checked I am using the right URL for a call and the right scope (using forge lint --fix):

permissions:
  scopes:
    - read:page:confluence

What else I have tried:

  • installing the latest version of used libraries (@forge/_____@latest)
  • redeploy the app
  • reinstall the app using forge install --upgrade
  • use forge tunnel
  • use api.asUser() and api.asApp() functions
  • change the runtime to nodejs18.x

but nothing helped.

Any idea what I am doing wrong, please?