Get all pages for a confluence user via Forge

shawn March 29, 2023

Here is my current code. It gets the first 250 page records (limit=250.) But it returns a 401 error ({ code: 401, message: 'Unauthorized; scope does not match' }) from the second request for the remaining pages.

here is my function ```

async function getAllPages(url: string, allPages: any[] = []): Promise<any[]> {
const confluenceUrl: Readonly<string> = url;
const routeUrl = route`${confluenceUrl}`;
//@prettier-ignore-next-line
let pagesFromApi = await api.asUser().requestConfluence(route`${url}`, {
headers: {
Accept: 'application/json',
},
});
console.log(await pagesFromApi.json(), 'this is pages from api');

console.log(
pagesFromApi.status,
pagesFromApi.statusText,
'this is pages from api',
);

const res = await pagesFromApi.json();
console.log(res.results, 'this is res');
console.log(url, 'this is the url');

allPages = allPages.concat(res.results);

if (pagesFromApi.headers.has('link')) {
const link = pagesFromApi.headers
.get('link')
?.match(/<([^>]+)>;\s*rel="next"/)?.[1]
.replace(/==/g, '');
if (link) {
console.log(`${link}`, 'this is the link');

allPages = await getAllPages(`${link.replace(/\s+/g, '')}`, allPages);
}
}

return allPages;
}

resolver.define('getText', async req => {
let pages = await getAllPages('/wiki/api/v2/pages');
// console.log(pages, 'this is pages');
console.log(pages.length, 'this is pages length');

return 'Hello, world!';
});

```
and here are the scopes ```

permissions:
scopes:
- read:confluence-space.summary
- read:confluence-props
- read:page:confluence
- read:confluence-content.all
- read:confluence-content.summary
- search:confluence
- read:confluence-content.permission
- read:confluence-user

```
Please help!

1 answer

0 votes
Alex Koxaras _Relational_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 2, 2023

Hi @shawn 

I would suggest to create a post at the developers' community, since you'll have more chances of getting a proper reply https://community.developer.atlassian.com/

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events