Fetches not working when Using Forge Fetch API

My app seems to be throwing an error when trying to fetch from any external API. I have followed the tutorial here Use an external OAuth 2.0 API with fetch
I have the following function:

  const myapi = await api.asUser().withProvider("gitlab", "gitlab-server")
  if (!await myapi.asUser().withProvider("gitlab", "gitlab-server").hasCredentials()) {
    await myapi.asUser().withProvider("gitlab", "gitlab-server").requestCredentials();
  }

My manifest.yml looks like this:

permissions:
  scopes:
    - read:jira-work
  external:
    fetch:
      backend:
        - 'https://cd.mycompany.com'

providers:
  auth:
    - key: gitlab
      name: Gitlab
      scopes:
        - 'profile'
        - 'read_user'
      type: oauth2
      clientId: 123
      remotes:
        - gitlab-server
      bearerMethod: authorization-header
      actions:
        authorization:
          remote: gitlab-server
          path: /oauth/authorize
        exchange:
          remote: gitlab-server
          path: /oauth/token
        revokeToken:
          remote: gitlab-server
          path: /oauth/token
        retrieveProfile:
          remote: gitlab-server
          path: /oauth/userinfo
          resolvers:
            id: sub
            displayName: email

however, I get the following error:

invocation: f79fe9594621785c index.run
ERROR   19:05:25.835  f79fe9594621785c  myapi.asUser is not a function
TypeError: myapi.asUser is not a function
    at fetchDetailsForMRs (webpack:///src/index.jsx:52)
    at async fetchMRsForIssue (webpack:///src/index.jsx:18)
    at async /tmp/tunnel8g1G0RaH6bsKV/index.js:14189:99
    at async /tmp/tunnel8g1G0RaH6bsKV/index.js:14349:21
    at async asyncMap (webpack:///node_modules/@forge/ui/out/reconcile.js:12)
    at async /tmp/tunnel8g1G0RaH6bsKV/index.js:14301:29
    at async asyncMap (webpack:///node_modules/@forge/ui/out/reconcile.js:12)
    at async /tmp/tunnel8g1G0RaH6bsKV/index.js:14363:23
    at async /tmp/tunnel8g1G0RaH6bsKV/index.js:14231:31

This appears to be identical to the code in the example…
If I replace the index code with this:

  const myapi = await api.asUser().withProvider("gitlab", "gitlab-server")
  if (!await api.asUser().withProvider("gitlab", "gitlab-server").hasCredentials()) {
    await api.asUser().withProvider("gitlab", "gitlab-server").requestCredentials();
  }

Then it takes me through the authentication process, but I just get an error stating could not retrieve access token from the provider
Any ideas on why it’s not working for me?

Thanks for trying out external authentication!

In that first code block there seems to have two issues:

  • You do not need to use await on the first line where you assign the wrapper to a variable.
  • It seems you are trying to call withProvider again on the withProvider wrapper. You should be able to just use the methods directly on it like below:
    const myapi = api.asUser().withProvider("gitlab", "gitlab-server")
    if (!await myapi.hasCredentials()) {
      await myapi.requestCredentials()
    }

For your issue about could not retrieve access token from the provider, can you see if one of these steps will resolve your problem?

  1. Ensure the client secret has been set using the configure providers CLI command.
  2. Confirm the exchange URL is correct in the manifest.yml file.

These were taken from the troubleshooting guide:
https://developer.atlassian.com/platform/forge/common-issues-with-external-authentication/#token-exchange-issues

3 Likes