【问题标题】:InternalOAuthError: Failed to obtain access token TwitchInternalOAuthError:无法获取访问令牌 Twitch
【发布时间】:2019-03-25 07:46:27
【问题描述】:

嘿,我处于静止状态。我不确定发生了什么,但我无法获得我的访问令牌。我正在尝试使用 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

【问题讨论】:

  • 抱歉,这是您的 twitch 帐户的访问令牌吗?还是您正在通过此令牌为您的用户创建访问令牌?
  • 您来自 twitch 的访问令牌在回调的响应标头中可用...所以为了得到它,您可以在那里寻找它。在邮递员中测试它
  • 这是一个用户令牌,所以我链接的这个重定向网址来自 twitch 我相信一旦我通过 twitch 登录。
  • 好的,它似乎对我有用(减去重定向网址)但这是你的问题,更改此行 passport.authenticate("twitch"));和这一行 passport.authenticate("twitch"));
  • 实际上没有用。问题是您正在尝试使用应用凭据进行身份验证并获取用户令牌...

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


【解决方案1】:

这里是您设置的工作副本:

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

【讨论】:

  • 嘿,感谢您的回复,但我仍然无法获得访问令牌:/.
  • 将更新帖子,无需稍后进行会话
  • 谢谢,非常感谢!
  • 更新了没有会话的代码,您也可以添加 JwT 策略来保护令牌。如果这对你有用,请点赞,谢谢
猜你喜欢
  • 2015-11-22
  • 2014-02-03
  • 2022-06-11
  • 1970-01-01
  • 1970-01-01
  • 2022-01-01
  • 2016-02-02
相关资源
最近更新 更多