InternalOAuthError: Failed to obtain access token Twitch

InternalOAuthError: Failed to obtain access token Twitch

嘿,我停滞不前了。我不确定发生了什么,但我无法获得我的访问令牌。我正在尝试使用 Passport 策略进行 twitch 身份验证。我 运行 遇到的错误是 InternalOAuthError: Failed to obtain an access token at 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

这是您设置的工作副本:

首先确保使用 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;