Need help to reslove 403 forbidden issue while integrating confluence

Hi everyone
I am currently working on the integration of conlfluence with my project.

async confluenceLogin(code?: string, redirect_uri?: string, state?: string) {
    if (!code) throw new Error("No code provided");
  
    const body = {
      grant_type: "authorization_code",
      client_id: process.env.CONFLUENCE_CLIENT_ID,
      client_secret: process.env.CONFLUENCE_CLIENT_SECRET,
      code,
      redirect_uri,
    };
  
    const headers = { "Content-Type": "application/json" };
  
    try {
      console.log("Step 1: Exchanging authorization code for access token...");
      // Step 1: Exchange the authorization code for an access token
      const tokenResponse = await axios.post("https://auth.atlassian.com/oauth/token", body, { headers });
      const accessToken = tokenResponse.data.access_token;
      console.log("Access token retrieved:", tokenResponse.data);
  
      console.log("Step 2: Fetching accessible resources to get Confluence URL...");
      const resourcesResponse = await axios.get("https://api.atlassian.com/oauth/token/accessible-resources", {
        headers: {
          Authorization: `Bearer ${accessToken}`,
          Accept: "application/json",
        },
      });
      console.log("Accessible resources response:", resourcesResponse.data);

      const confluenceResource = resourcesResponse.data.find(
        (resource: any) => resource.scopes.includes("read:confluence-content.all")
      );

      if (!confluenceResource) {
        console.error("No Confluence resource found for the user. Accessible resources:", resourcesResponse.data);
        throw new Error("No Confluence resource found for the user.");
      }

      const baseUrl = confluenceResource.url;
      console.log("Base URL constructed:", baseUrl);
  
      console.log("Step 3: Fetching available spaces...");
      // Step 3: Fetch available spaces
      const spacesResponse = await axios.get(`${baseUrl}/wiki/rest/api/space`, {
        headers: {
          Authorization: `Bearer ${accessToken}`,
          Accept: "application/json",
        },
      });
      console.log("Spaces response:", spacesResponse.data);
  
      const spaceKey = spacesResponse.data.results[0]?.key; // Select the first space as an example
      if (!spaceKey) {
        throw new Error("No spaces found for the user.");
      }
      console.log("Space key selected:", spaceKey);
  
      console.log("Step 4: Fetching pages from the selected space...");
      // Step 4: Fetch pages from the selected space
      const pagesResponse = await axios.get(
        `${baseUrl}/wiki/rest/api/content?spaceKey=${spaceKey}&expand=title`,
        {
          headers: {
            Authorization: `Bearer ${accessToken}`,
            Accept: "application/json",
          },
        }
      );
      console.log("Pages response:", pagesResponse.data);
  
      const pageIds = pagesResponse.data.results.map((page: any) => ({
        id: page.id,
        title: page.title,
      }));
      console.log("Page IDs retrieved:", pageIds);
  
      return {
        accessToken,
        baseUrl,
        spaceKey,
        pageIds,
      };
    } catch (error: any) {
      console.error("Error during Confluence login:", error.response?.data || error.message);
      throw new Error("Error during Confluence login");
    }
  }

The issue is from step 3.
I got the following error.

message: 'Current user not permitted to use Confluence',
statusCode: 403

I defined scopes: read:confluence-content.all read:confluence-space.summary read:me offline_access

I hope to get help.
Thank you.

@antman,

I think the problem is the URL construction. The URL found in accessible-resources is not a baseUrl for API calls. Instead, the rules for URL construction are to use the cloudId in this template https://api.atlassian.com/ex/confluence/{cloudid}/{api}.

Thank you @ibuchanan , you are right.
I can move on the next steps.
Best regards
@antman

1 Like