How to use the simple-oauth2 lib to make a request for a token?

26 Views Asked by At

When I make the request with curl I receive the token.

$ curl --location 'https://myapi.com/oauth/token' --header 'Content-Type: application/x-www-form-urlencoded' --data-urlencode 'client_id=myclientid' --data-urlencode 'client_secret=myclientsecret'

When using simple-oauth2 I get unauthorized:

const  { ClientCredentials} = require('simple-oauth2');
const config = {
    client: {
      id: "myclientid",
      secret: "myclientsecret",
    },
    auth: {
      tokenHost: "https://myapi.com/",
      tokenPath: "/oauth/token"
    }
  };

async function run() {
    const client = new ClientCredentials(config);
    try {
        const accessToken = await client.getToken();
        console.log(accessToken)
    } catch (error) {
      console.log('Access Token error', error.message);
    }
  }
  
  run();

After running this piece of code I get unauthorized and the following payload in error:

payload: {
      error: 'invalid_client',
      error_description: 'Invalid client or Invalid client credentials'
}

I've already tried to use:

const config= {
    /*
       your existing config
    */
    options: {
        authorizationMethod: 'body'
    }
}

And the same problem occours.

I expected it to work since it is a simple oauth2 request.

I've already tried to use something like the config suggested here:

how to use the simple-oauth2 library to make request application/x-www-form-urlencoded?

0

There are 0 best solutions below