内部OAuth错误:无法获取访问令牌Twitch



嘿,我处于停滞状态。我不确定发生了什么,但我无法获取我的访问令牌。我正在尝试使用护照策略进行抽搐身份验证。我遇到的错误是内部OAuth错误:无法在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((( 启用会话策略; 为了包括序列化和反序列化,这是您的代码将从服务器获取 authtoken 的地方。

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;

最新更新