Unable to retrieve page information via Forge API

Hi,

I am stuck trying to pull/fetch page information such as:

  • Title Page
  • Date created
  • Date modified
  • Person who created page
    etc.

What I am trying to do, is build a forge macro on confluence cloud, that when the page is loaded, these fields are automatically displayed on the page, without user intervention.

This is what I am working with so far.

import api, { route } from "@forge/api";
import ForgeUI, { render, Fragment, Text, Macro, useProductContext, useState } from '@forge/ui';

const fetchCommentsForContent = async (contentId) => {
  const res = await api
    .asUser()
    .requestConfluence(route`/rest/api/content/${contentId}/child/comment`);

  const data = await res.json();
  return data.results;
};

const fetchPageTitleForContent = async (contentId, title_) => {
  const res = await api
    .asUser()
    .requestConfluence(route`/rest/api/content/${contentId}/?title=${title_}`);

  const data = await res.json();
  return data.results;
};


const App = () => {
  const context = useProductContext();
  const [comments] = useState(async () => await fetchCommentsForContent(context.contentId));
  const pageTitle = useState(async () => await fetchPageTitleForContent(context.contentId, context.title));
  //console.log(`Number of comments on this page: ${comments.length}`);

  return (
    <Fragment>
      <Text>Number of comments on this page: {comments.length}, {pageTitle}</Text>
    </Fragment>
  );
};

export const run = render(
  <Macro
    app={<App />}
  />
);

However I cannot seem to get it to work at all, can someone please advise?
Thank you.

p.s. I am still a beginner with API’s

Are you receiving any results here? If not data.results may be returning undefined, meaning {comments.length} will not display what you want?

const data = await res.json();
console.log(data) // What do you see?
console.log(data.results) // Any results?

From what you have provided, using UI-Kit, the API’s are being used correctly. Just for arguments sake, have you recently re-deployed (If you have changed permissions required in your manifest file you will need to use “forge deploy” to provide the updated manifest file.

Another thing to check is if your API calls are returning as expected?

const res = await api
    .asUser()
    .requestConfluence(route`/rest/api/content/${contentId}/child/comment`);

console.log(`Response is: ${res.status}, ${res.statusText}`); //200 OK?
1 Like

Hi Denny,

Thanks for the reply, so the section with the comment related stuff:

.requestConfluence(route`/rest/api/content/${contentId}/child/comment`);

was from the Forge tutorials and that works, i can display the number of comments, however thats not what I need for my task :frowning:

I’m trying to pull the page properties so that i can output the page title, date created etc. as a text. I’ve been trying with this code, but still no luck:

const getProperty = async (contentId) => {
  // await spaceApi.set(key, value);
  // TODO: Replace this user impersonation workaround when the spaceApi works as it should
  const res = await api.asApp().requestConfluence(route`/wiki/rest/api/content/${contentId}/`, {
  headers: {
    'Accept': 'application/json'
	  }
	});
  const data = await res.json();
  return data.results;
};

const App = () => {
  const context = useProductContext();
  const [comments] = useState(async () => await fetchCommentsForContent(context.contentId));

  const response = useState(async () => await getProperty(context.contentId));
};

Also quick question, with the console.log, where is that outputted? I’m not using the tunnel because that connection didnt work, so i’ve been trying to see the results directly from the confluence page im testing.

Also quick question, with the console.log, where is that outputted? I’m not using the tunnel because that connection didnt work

The console.logs would appear in the tunnel. I took the liberty of testing your code provided, the API request (res) is returning the following:

{ code: 401, message: 'Unauthorized; scope does not match' }

Plus context.contentId is ‘undefined’. You may need to look at getting your Forge Tunnel working to aid with debugging the issue.

Ok no worries thanks for that.

Do you have any sample code that I could use that would help me fetch the page properties though like page title and so on?
Or was I on the right track? I’m new to this forge language so i’m learning as i go.

The example endpoint should work as you had it (given the correct permissions are placed in your manifest file), you are on the right track for Forge UI-Kit (However, UIKit is limited, meaning if you cannot achieve what you require, it may be worth attempting to get the same result using Forge CustomUI)

await api.asUser().requestConfluence(route`/rest/api/content/${contentId}/child/comment`);

See Manifest and Confluence Cloud Rest API documentation.

Is it possible you are able to use one of the Endpoints for retrieving Space content? GET /wiki/rest/api/space to retrieve the child pages, which should contain the JSON information you are looking for (more information in the ‘Space’ section in the confluence API docs)?

1 Like

Hello, having the same issue after updating @forge/api from version 1.2.0 to version 2.2.1
I have added the route formatter to all my requestConfluence calls, and it seems that is is url encoding subroutes.
This:

route`/rest/api/space/${spaceKey}/property/${propertyName}`

is giving:

/rest/api/space%2F[spaceKey]%2Fproperty%2F[propertyName]

And after attempting the call to requestConfluence with that route, I’m getting the Unauthorized; scope does not match error

Ok, I see what happens now. My example was actually inaccurate. I’m not doing

route`/rest/api/space/${spaceKey}/property/${propertyName}`

But

route`/rest/api/${path}`

And that’s why the entire subpath is being encoded.

We have all the REST requests to Confluence centralized in a single method where we parse query parameters and handle errors, so changing this will be a little more challenging than I initially estimated.