simple-oauth2 icon indicating copy to clipboard operation
simple-oauth2 copied to clipboard

401 on token exchange

Open Maneshme opened this issue 2 years ago • 0 comments

Context

  • node version: v18.12.1
  • module version: 5.0.0
  • environment (e.g. node, browser, native): Node
  • any other relevant information:

How can we help?

I was trying do a token exchange to authenticate with the pinniped-supervispor which requires us to pass a client-id and client secret as basic authorization, and I need to pass the data in payload as form data. I got this far with your docs but I keep getting a 401 error

const { code, state, scope } = req.query;
  const auth  =  Buffer.from(`${authConfig.client.id}:${process.env['CLIENT_SECRET']}`).toString('base64');
  const authData = readCachedAuthData(state)
    const params = {
      code,
      redirect_uri: authData.redirect,
      scope: scope
    };
    const options = {
        headers: {
          accept: 'application/json',
          authorization : `Basic ${auth}`
        },
        payload: {
          grant_type: 'authorization_code',
          code: req.query.code,
          redirect_uri: authData.redirect,
          code_verifier: authData.verfier
        },
        scopeSeparator: ''
      };
    try {
      const accessToken = await client.getToken(params, options);

      console.log('The resulting token: ', accessToken.token);

      return res.status(200).json(accessToken.token);
    } catch (error) {
      console.error('Access Token Error', error.message);
      return res.status(error.output.statusCode).json(error.message);
    }

How do I pass the credentials and how do I pass the form data??

Maneshme avatar May 23 '23 15:05 Maneshme