How to get rid of 400 bad request error in simple-oauth2 getToken method?

774 views Asked by At

I am trying to create an application where i want to fetch Outlook mails. I am using using simple OAuth 2.0 for the access token. I am successfully getting the code. When I am trying to get the access token in exchange of code I am getting 400 bad request error.

const tokenConfig = {
  code: auth_code,
  redirect_uri: redirectUri,
  scope: scopes.join(' ')
};
try {
  const result = await oauth2.authorizationCode.getToken(tokenConfig);
  const accessToken = oauth2.accessToken.create(result);
} catch (error) {
   console.log('Access Token Error', error.message);
}

Can you help what wrong I am doing. Please comment for more information.

1

There are 1 answers

2
jmjohnson85 On

For me the solution was to set ModuleOptions.options.authorizationMethod to 'body':

const oAuthConfig = {
    client: {
        id: 'xxxxxxxxxx',
        secret: 'xxxxxxxxxxxxxxxx'
    },
    auth: {
        tokenHost: 'https://oauthhost.example.com/common/',
        tokenPath: 'authtokenserver/token',
        authorizePath: 'authtokenserver/authorize'
    },
    options: {
        authorizationMethod: 'body'
    }
}

Edit [6/12/2023]: Here's a link to the API documentation for simple-oauth2. I've also copied the bits that pertain to this answer below:

  • options additional options to setup how the module perform requests
    • authorizationMethod - Method used to send the client.id/client.secret authorization params at the token request. Valid options are header or body. If set to body, the bodyFormat option will be used to format the credentials. Defaults to header