Helix API - NodeJS - Auth + User Info

I found this sample https://github.com/twitchdev/authentication-samples/blob/master/node/index.js and it works with Kraken. Then I tried to use Helix (I modified the code), but the ‘users’ API returns ‘Unauthorized’, and I guess the token is not good (is it an application token?). BTW any plan to update the sample for Helix API?

Here is my code:
`const PORT = process.env.PORT || 5000

// Define our dependencies
var express = require(‘express’);
var session = require(‘express-session’);
var passport = require(‘passport’);
var OAuth2Strategy = require(‘passport-oauth’).OAuth2Strategy;
var request = require(‘request’);
var handlebars = require(‘handlebars’);

// Define our constants, you will change these with your own
const TWITCH_CLIENT_ID = ‘XXX’;
const TWITCH_SECRET = ‘XXX’;
const SESSION_SECRET = ‘XXX’;
const CALLBACK_URL = ‘XXX/auth/twitch/callback’; // You can run locally with - http://localhost:3000/auth/twitch/callback

// Initialize Express and middlewares
var app = express();
app.use(session({secret: SESSION_SECRET, resave: false, saveUninitialized: false}));
app.use(express.static(‘public’));
app.use(passport.initialize());
app.use(passport.session());

// Override passport profile function to get user profile from Twitch API
OAuth2Strategy.prototype.userProfile = function(accessToken, done) {
var options = {
//url: ‘https://api.twitch.tv/kraken/user’, // OLD
url: ‘https://api.twitch.tv/helix/users’,
method: ‘GET’,
headers: {
//‘Client-ID’: TWITCH_CLIENT_ID, // OLD
//‘Accept’: ‘application/vnd.twitchtv.v5+json’, // OLD
//‘Authorization’: 'OAuth ’ + accessToken // OLD
‘Authorization’: 'Bearer ’ + accessToken
}
};

request(options, function (error, response, body) {
    if (response && response.statusCode == 200) {
        done(null, JSON.parse(body));
    } else {
        done(JSON.parse(body));
    }
});

}

passport.serializeUser(function(user, done) {
done(null, user);
});

passport.deserializeUser(function(user, done) {
done(null, user);
});

passport.use(‘twitch’, new OAuth2Strategy({
//authorizationURL: ‘https://api.twitch.tv/kraken/oauth2/authorize’, // OLD
authorizationURL: ‘https://id.twitch.tv/oauth2/authorize’,
//tokenURL: ‘https://api.twitch.tv/kraken/oauth2/token’, // OLD
tokenURL: ‘https://api.twitch.tv/oauth2/token’,
clientID: TWITCH_CLIENT_ID,
clientSecret: TWITCH_SECRET,
callbackURL: CALLBACK_URL,
state: true
},
function(accessToken, refreshToken, profile, done) {
profile.accessToken = accessToken;
profile.refreshToken = refreshToken;

    // Securely store user profile in your DB
    //User.findOrCreate(..., function(err, user) {
    //  done(err, user);
    //});

    done(null, profile);
}

));

// Set route to start OAuth link, this is where you define scopes to request
//app.get(‘/auth/twitch’, passport.authenticate(‘twitch’, { scope: ‘user_read’ })); // OLD
app.get(‘/auth/twitch’, passport.authenticate(‘twitch’, { scope: ‘user:read:email’ }));

// Set route for OAuth redirect
app.get(‘/auth/twitch/callback’, passport.authenticate(‘twitch’, { successRedirect: ‘/’, failureRedirect: ‘/’ }));

// Define a simple template to safely generate HTML with values from user’s profile
var template = handlebars.compile(`

Twitch Auth Sample
Access Token{{accessToken}}
Refresh Token{{refreshToken}}
Display Name{{display_name}}
Bio{{bio}}
Image{{logo}}
`);

// If user has an authenticated session, display it, otherwise display link to authenticate
app.get(‘/’, function (req, res) {
if(req.session && req.session.passport && req.session.passport.user) {
res.send(template(req.session.passport.user));
} else {
res.send(‘Twitch Auth Sample’);
}
});

app.listen(PORT, function () {
console.log(‘Twitch auth sample listening…’)
});`

1 Like
tokenURL: ‘https://api.twitch.tv/oauth2/token’,

should be using id.twitch.tv

It works, thanks!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.