InternalOAuthError:无法获取访问令牌Twitch

时间:2018-10-20 18:39:46

标签: javascript node.js oauth oauth-2.0 twitch

嘿,我处于停滞状态。我不确定发生了什么,但无法获取访问令牌。我正在尝试使用Passport策略进行抽搐身份验证。我遇到的错误是InternalOAuthError:无法通过Strategy.OAuth2Strategy._createOAuthError获取访问令牌。我在做什么错了?

护照策略

passport.use(
    new TwitchStrategy({
        clientID: keys.twitchClientID,
        clientSecrect: keys.twitchClientSecrect,
        // callbackURL:'/auth/twitch/callback',
        callbackURL:'http://127.0.0.1:5000/auth/twitch/callback',
        scope: "user:read:email analytics:read:games",
        proxy: true
    }, (accessToken, refreshToken, profile, done) => {
        console.log(accessToken);
        console.log(profile);
    })
)

身份验证路由器

router.get("/twitch", passport.authenticate("twitch.js"));

router.get(
  "/twitch/callback",
  passport.authenticate("twitch.js", { failureRedirect: "/" }),
  (req, res) => {
    // Successful authentication, redirect home.
    res.redirect("/");
  }
);

NPM软件包

https://www.npmjs.com/package/passport-twitch.js

从身份验证重定向URL

http://localhost:5000/auth/twitch/callback?code=xqp1au3zqigezj8dzeslcvih8mqn6x&scope=user%3Aread%3Aemail+analytics%3Aread%3Agames

1 个答案:

答案 0 :(得分:0)

这是您设置的工作副本:

首先请确保使用app.use(passport.initialize())在会话中启用该策略; 并包括序列化和反序列化,这是您的代码从服务器获取身份验证令牌的地方。

const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const Twitch = require('./model');
const mongoose = require("mongoose");
const passport = require('passport');
const twitchStrategy = require("passport-twitch").Strategy;


mongoose
  .connect(
    "<mongourl>"
  )
  .then(() => {
    console.log("connected to database!");
  })
  .catch(() => {
    console.log("connection failed");
  });

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.use((req, res, next) => {
  res.setHeader("Access-Control-Allow-Origin", "*");
  res.setHeader(
    "Access-Control-Allow-Headers",
    "Origin, X-requested-With, Content-Type, Accept"
  );
  res.setHeader(
    "Access-Control-Allow-Methods",
    "GET, POST, PATCH, PUT, DELETE, OPTIONS, PUT"
  );

  next();
});
app.use(passport.initialize());

passport.use(new twitchStrategy({
  clientID: "<clientid>",
  clientSecret: "<clientsecret>",
  callbackURL: "http://localhost:3000/auth/twitch/callback",
  scope: "user_read"
},
function(accessToken, refreshToken, profile, done) {
 
  twitch.save({ twitchId: profile.id }, function (err, user) {
    console.log(user);
    return done(err, user);
  });
}

));

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

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

app.get("/", function (req, res) {
  res.send(`<html><head></head><body>Here</body></html>`);
});


app.get("/auth/twitch", passport.authenticate("twitch"));


 app.get("/auth/twitch/callback" ,passport.authenticate("twitch"), function(req, res) {

 
  res.redirect("/");
});





module.exports = app;

无会话

const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const passport = require('passport');
const twitchStrategy = require("passport-twitch").Strategy;
const axios = require('axios');
const twitchAxios = axios.create({
  baseURL: 'http://localhost:3000',
  timeout: 1000,
  headers:{
    "Content-type": "application/json",
        "Accept": "application/json",
        "Authorization": "bearer TOKEN" 
  }
});


mongoose
  .connect(
    ""
  )
  .then(() => {
    console.log("connected to database!");
  })
  .catch(() => {
    console.log("connection failed");
  });

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.use((req, res, next) => {
  res.setHeader("Access-Control-Allow-Origin", "*");
  res.setHeader(
    "Access-Control-Allow-Headers",
    "Origin, X-requested-With, Content-Type, Accept"
  );
  res.setHeader(
    "Access-Control-Allow-Methods",
    "GET, POST, PATCH, PUT, DELETE, OPTIONS, PUT"
  );

  next();
});
app.use(passport.initialize());

passport.use(new twitchStrategy({
  clientID: "",
  clientSecret: "",
  callbackURL: "http://localhost:3000/auth/twitch/callback",
  scope: "user_read"
},
function(accessToken, refreshToken, profile, done) {

  twitch.save({ twitchId: profile.id }, function (err, user) {
    return done(err, user);
  });
}

));


app.get("/", function (req, res) {
  res.send(`<html><head></head><body>Here</body></html>`);
});


app.get("/auth/twitch", passport.authenticate("twitch",{session: false}));


 app.get("/auth/twitch/callback", function(req, res) {
   twitchAxios.get('/').then(console.log)


 
  res.redirect("/");
});





module.exports = app;