withProvider not substituting remote name when using external auth in Forge app
Asked by Andy Sturrock:

I want to send the contents of a Confluence page to one of my APIs with the authorization done with Slack. I have the following resolver code:  

```
resolver.define('summarise', async (req) => {

  const pageId = req.payload.pageId;

  const confluenceAPIResult = await api.asUser().requestConfluence(route`/wiki/api/v2/pages/${pageId}?body-format=atlas_doc_format`);

  const json = await confluenceAPIResult.json();

  const body = JSON.parse(json.body.atlas_doc_format.value);

  const atlasBotAPI = api.asUser().withProvider('slack', 'atlasbot-api');

  if(!await atlasBotAPI.hasCredentials()) {

    await atlasBotAPI.requestCredentials();

  }

  const accounts = await atlasBotAPI.listAccounts();

  console.log(`accounts: ${inspect(accounts, false, null, true)}`);

  const account = await atlasBotAPI.getAccount();

  console.log(`account: ${inspect(account, false, null, true)}`);

  const options = {

    method: 'POST',

    body: JSON.stringify(body)

  };

  try {

    const atlasBotAPIesult = await atlasBotAPI.fetch(route`/0_0_1/atlas-webhook`, options);

    console.log(`slackResult: ${inspect(atlasBotAPIesult, false, null)}`);

  }

  catch(error) {

    console.error(error);

    console.log(`error: ${util.inspect(error, false, null, true)}`);

  }

});


```

My manifest.yml looks like this:  

```
modules:

  confluence:contentBylineItem:

    - key: atlasbot-content-byline-item

      resource: main

      resolver:

        function: resolver

      render: native

      title: atlasbot

  function:

    - key: resolver

      handler: index.handler

      providers:

        auth:

          - slack

resources:

  - key: main

    path: src/frontend/index.tsx

app:

  runtime:

    name: nodejs18.x

  id: ari:cloud:ecosystem::app/my-id

permissions:

  scopes:

    - read:page:confluence

  external:

    fetch:

      backend:

        - remote: atlasbot-api

        - remote: slack-com

providers:

  auth:

    - key: slack

      name: Slack

      scopes:

        - 'openid'

        - 'email'

        - 'profile'

      type: oauth2

      clientId: 'my-slack-app-client-id'

      remotes:

        - slack-com

      bearerMethod: authorization-header

      actions:

        authorization:

          remote: slack-com

          path: /openid/connect/authorize

        exchange:

          remote: slack-com

          path: /api/openid.connect.token

        retrieveProfile:

          remote: slack-com

          path: /api/openid.connect.userInfo

          resolvers:

            id: sub

            displayName: name

            avatarUrl: picture

remotes:

  - key: atlasbot-api

    baseUrl: https://atlasbot.example.com

  - key: slack-com

    baseUrl: https://slack.com
```

<br />

(I've obfuscated values here including the baseUrl for atlasbot-api).  
The Slack OAuth authentication works fine and I get sensible values back for the account.  
The fetch throws a 400 error with "PROXY_ERR: Forge platform failed to process runtime HTTP request - 400 - INVALID_REMOTE"  
I've stepped through the debugger and the error is because it's trying to POST to "<https://atlasbot-api/?path=%2F0_0_1%2Fatlas-webhook>" rather than "https://atlasbot.example.com/0_0_1/atlas-webhook" that I would expect. The function  
fetchRemote at line 35 of [@Forge](/t5/user/viewprofilepage/user-id/4918321)/api/out/api/fetch.js seems to be just using the name of the remote rather than looking up the remote's baseUrl.  
I've looked at <https://bitbucket.org/atlassian/forge-external-auth-google-example/src/master/> and I can't see what I am doing differently to that.  
Please help!


Answers:
author: Andy Sturrock
body: I have posted this to <https://community.developer.atlassian.com/t/withprovider-not-substituting-remote-name-when-using-external-auth-in-forge-app/80577> as I think that's a more appropriate forum.


Tags: cloud

confluence-cloud

forge