# Helix API - NodeJS - Auth + User Info

**URL:** <https://discuss.dev.twitch.com/t/helix-api-nodejs-auth-user-info/16640>\
**Category:** API\
**Created:** [July 16, 2018, 6:13pm UTC](https://discuss.dev.twitch.com/t/helix-api-nodejs-auth-user-info/16640 "2018-07-16T18:13:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![R3V0LUT10N909](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/r3v0lut10n909/32/4834_2.png) [@R3V0LUT10N909](https://discuss.dev.twitch.com/u/R3V0LUT10N909)\
**Post date:** [July 16, 2018, 6:13pm UTC](https://discuss.dev.twitch.com/t/helix-api-nodejs-auth-user-info/16640/1 "2018-07-16T18:13:55Z")

</div>

I found this sample [https://github.com/twitchdev/authentication-samples/blob/master/node/index.js](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](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](https://api.twitch.tv/kraken/user)’, // OLD  
url: ‘[https://api.twitch.tv/helix/users](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](https://api.twitch.tv/kraken/oauth2/authorize)’, // OLD  
authorizationURL: ‘[https://id.twitch.tv/oauth2/authorize](https://id.twitch.tv/oauth2/authorize)’,  
//tokenURL: ‘[https://api.twitch.tv/kraken/oauth2/token](https://api.twitch.tv/kraken/oauth2/token)’, // OLD  
tokenURL: ‘[https://api.twitch.tv/oauth2/token](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](https://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[![](https://d2opxh93rbxzdn.cloudfront.net/original/2X/0/05c21e05a37ee1291c69a747959359dab90c0af3.png)](https://discuss.dev.twitch.com/auth/twitch)’);  
}  
});

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

---

<div class="post-metadata">

**Author:** ![3ventic](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/3ventic/32/298_2.png) [@3ventic](https://discuss.dev.twitch.com/u/3ventic)\
**Post date:** [July 16, 2018, 6:32pm UTC](https://discuss.dev.twitch.com/t/helix-api-nodejs-auth-user-info/16640/2 "2018-07-16T18:32:54Z")

</div>

```auto
tokenURL: ‘https://api.twitch.tv/oauth2/token’,

```

should be using `id.twitch.tv`

---

<div class="post-metadata">

**Author:** ![R3V0LUT10N909](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/r3v0lut10n909/32/4834_2.png) [@R3V0LUT10N909](https://discuss.dev.twitch.com/u/R3V0LUT10N909)\
**Post date:** [July 17, 2018, 2:32pm UTC](https://discuss.dev.twitch.com/t/helix-api-nodejs-auth-user-info/16640/3 "2018-07-17T14:32:23Z")

</div>

It works, thanks!

---

<div class="post-metadata">

**Author:** ![system](https://d2opxh93rbxzdn.cloudfront.net/original/2X/0/05909729875cce3b53e59d5561c4d23bb14948fc.png) [@system](https://discuss.dev.twitch.com/u/system)\
**Post date:** [August 16, 2018, 2:32pm UTC](https://discuss.dev.twitch.com/t/helix-api-nodejs-auth-user-info/16640/4 "2018-08-16T14:32:27Z")

</div>

Closed on August 16, 2018, 2:32pm UTC

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