We are already doing the dance, and every thing works, except we don’t get back an OAuth token. We get an “access code” that doesn’t work, but the codes we get from the tmi OAuth generator DO work. Here is the code
let responseStr = “https://id.twitch.tv/oauth2/authorize?response_type=code&token+id_token&client_id=” + twitchCltId + “&redirect_uri=” + redirectUri + “&scope=chat_login+viewing_activity_read+openid+user:read:email+bits:read&state=” + randState;
// At the redirect uri:
if (req.query.state == randState && req.query.code) {
const code = req.query.code;
request({
uri: “https://id.twitch.tv/oauth2/token?client_id=” + twitchCltId + “&client_secret=” + twitchSecret + “&grant_type=authorization_code&redirect_uri=” + redirectUri + “&code=” + req.query.code,
method: “POST”,
timeout: 10000,
followRedirect: true,
maxRedirects: 10
}, function(error, response, body) {
// Todo: Validate the response id_token
var jsonResponse = JSON.parse(body);
var idToken = jsonResponse["id_token"];
var accessToken = jsonResponse["access_token"];
var refreshToken = jsonResponse["refresh_token"];
var postUrl = "https://api.twitch.tv/helix/users";
var options = {
method: 'GET',
url: postUrl,
timeout: 10000,
headers: {
"Authorization": "Bearer ".concat(accessToken)
}
};
request(options, function(error,response,body){
// Store in the db
console.log("res: ", response);
if(error) {
console.log("error in request", error);
res.send(error);
return
}
var jsonResponse = JSON.parse(body);
var userData = jsonResponse.data[0];