Problems authenticating at Github with OAuth provider

Hi!

I’m trying to authenticate at Github using an OAuth provider. When requesting credentials using

const github = api.asUser().withProvider('github', 'github-api');
if (!(await github.hasCredentials())) {
  await github.requestCredentials();
}

I get this error:

[NEEDS_AUTHENTICATION_ERR: Authentication Required] {
  status: 401,
  serviceKey: 'github',
  options: { scopes: undefined, isExpectedError: true }

This sounds like the client secret might be missing, but I have set it up using
forge providers configure. My provider definition looks like this:

providers:
  auth:
    - key: github
      name: Github
      remotes:
        - github-base
        - github-api
      scopes:
        - repo
        - user
      type: oauth2
      clientId: ***
      bearerMethod:
        type: authorization-header
      actions:
        authorization:
          remote: github-base
          path: /login/oauth/authorize
        exchange:
          remote: github-base
          path: /login/oauth/access_token
          resolvers:
            accessToken: access_token
        retrieveProfile:
          remote: github-api
          path: /user
          resolvers:
            id: id
            displayName: login

remotes:
  - key: github-api
    baseUrl: https://api.github.com
  - key: github-base
    baseUrl: https://github.com

permissions:
  external:
    fetch:
      backend:
        - https://github.com
        - https://api.github.com

Anyone who can you help me to figure out what the problem is?

Thanks!

Hi There!

Could you recheck the following and see if the issue still exists

  1. The scopes might not be set up correctly according to the documentation. Please have a look at the following references to ensure they’re defined properly and are accepted by the provider:
  1. The provider is configured correctly for the function. Here’s a quick example for reference:
function:
  - key: main
    handler: index.handler
    providers:
      auth:
        - github

Hi,

it turned out that the code block calling requestCredentials() was in a try/catch block, but it is expected to throw. So removing the surrounding try/catch solved the issue.

Thanks!

1 Like