How to oauth with node?

Hi guys, I’m new to node and Oauth (in general) I was wondering what was incorrect in my code? Thank you for the help.

app.get(’/user’, (req, res, body) => {

const getAuth = (url, callback) => {

    const options2 = {

    url: process.env.GET_AUTH,

        body: {

            redirect_uri: process.env.REDIRECT_URI,

            response_type: "token",

            scope: "channel:read:subscriptions bits:read user:read:follows"

        }

    }

   

request.get(options2, (err, res, body) => {

    if(err) {

        return console.log(err)

    }

    console.log(`Status: ${res.statusCode}`);

    console.log(body);

    callback(res);

    });

}

var auth = '';

getAuth(process.env.GET_AUTH, (res) => {

    auth = res.body;

    console.log(auth);

});

});

You tried to “get”/fetch the URL rather than redirect the website user to the URL

Here is an example nodeJS user access generator using Express (for web serving) and Got (for web requests)

The general rule for oAuth is:

  • construct a <a href=" to Twitch oAuth Entry
  • user clicks that link
  • user goes to Twitch
  • user accepts (or declines) the link
  • user comes back to your REDIRECT_URI with a ?code in the parameters
  • you exchange the ?code for an access token you can use

The linked example will do this and then perform a user lookup to get the user for that token

1 Like