本文介绍了InternalOAuthError:无法获取访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮助我解决链接 GitHub 中以下代码的问题oauth2-provider 服务器和passport-oauth2 消费者

在我使用 http://localhost:8082 登录并到达我的回调 URL 后:http://localhost:8081/auth/provider/callback,报错

var express = require('express'), 护照 = 要求('护照'), util = require('util'), TwitterStrategy = require('passport-twitter').Strategy;var TWITTER_CONSUMER_KEY = "--insert-twitter-consumer-key-here--";var TWITTER_CONSUMER_SECRET = "--insert-twitter-consumer-secret-here--";护照.serializeUser(功能(用户,完成){完成(空,用户);});护照.deserializeUser(功能(对象,完成){完成(空,对象);});护照.使用(新推特策略({消费者密钥:TWITTER_CONSUMER_KEY,消费者秘密:TWITTER_CONSUMER_SECRET,callbackURL:http://127.0.0.1:3000/auth/twitter/callback"},功能(令牌,tokenSecret,配置文件,完成){//异步验证,为了效果...process.nextTick(function () {返回完成(空,配置文件);});}));var app = express.createServer();//配置快递app.configure(function() {app.set('views', __dirname + '/views');app.set('视图引擎', 'ejs');app.use(express.logger());app.use(express.cookieParser());app.use(express.bodyParser());app.use(express.methodOverride());app.use(express.session({secret: 'keyboard cat' }));app.use(passport.initialize());app.use(passport.session());app.use(app.router);app.use(express.static(__dirname + '/public'));});app.get('/', function(req, res){res.render('index', { user: req.user });});app.get('/account', ensureAuthenticated, function(req, res){res.render('account', { user: req.user });});app.get('/login', function(req, res){res.render('登录', { 用户: req.user });});app.get('/auth/twitter',护照.认证('推特'),功能(请求,资源){//请求将被重定向到 Twitter 进行身份验证,所以这个//函数不会被调用.});app.get('/auth/twitter/callback',passport.authenticate('twitter', { failureRedirect: '/login' }),功能(请求,资源){res.redirect('/');});app.get('/logout', function(req, res){req.logout();res.redirect('/');});app.listen(3000);功能确保认证(请求,资源,下一个){if (req.isAuthenticated()) { return next(); }}res.redirect('/登录')}

InternalOAuthError:获取访问令牌失败

我该如何解决这个问题?

解决方案

我在尝试使用 Passport-oauth2 时遇到了类似的问题.正如您所观察到的,错误消息没有什么帮助:

InternalOAuthError:获取访问令牌失败在 OAuth2Strategy._createOAuthError (node_modules/passport-oauth2/lib/strategy.js:382:17)在 node_modules/passport-oauth2/lib/strategy.js:168:36在 node_modules/oauth/lib/oauth2.js:191:18在 ClientRequest.(node_modules/oauth/lib/oauth2.js:162:5)在emitOne (events.js:116:13)在 ClientRequest.emit (events.js:211:7)在 TLSSocket.socketErrorListener (_http_client.js:387:9)在emitOne (events.js:116:13)在 TLSSocket.emit (events.js:211:7)在emitErrorNT (internal/streams/destroy.js:64:8)

我发现建议对passport-oauth2做一些小改动:

--- a/lib/strategy.js+++ b/lib/strategy.js@@ -163,7 +163,10 @@ OAuth2Strategy.prototype.authenticate = function(req, options) {self._oauth2.getOAuthAccessToken(code, params,功能(错误,访问令牌,刷新令牌,参数){- if (err) { return self.error(self._createOAuthError('Failed to get access token', err));}+ 如果(错误){+ console.warn("获取访问令牌失败:", err);+ return self.error(self._createOAuthError('获取访问令牌失败', err));+ }

一旦我这样做了,我收到了一条更有帮助的错误消息:

获取访问令牌失败:{错误:自签名证书在 TLSSocket.(_tls_wrap.js:1103:38)在emitNone (events.js:106:13)在 TLSSocket.emit (events.js:208:7)在 TLSSocket._finishInit (_tls_wrap.js:637:8)在 TLSWrap.ssl.onhandshakedone (_tls_wrap.js:467:38) 代码:'DEPTH_ZERO_SELF_SIGNED_CERT' }

就我而言,我认为根本原因是我正在测试的授权服务器使用的是自签名 SSL 证书,我可以通过添加以下行来解决此问题:

require('https').globalAgent.options.rejectUnauthorized = false;

Can anyone help me with what is wrong with the below code in the link GitHuboauth2-provider server with passport-oauth2 consumer

After I login with http://localhost:8082 and reach my callback URL:http://localhost:8081/auth/provider/callback, it throws an error

var express = require('express')
  , passport = require('passport')
  , util = require('util')
  , TwitterStrategy = require('passport-twitter').Strategy;

var TWITTER_CONSUMER_KEY = "--insert-twitter-consumer-key-here--";
var TWITTER_CONSUMER_SECRET = "--insert-twitter-consumer-secret-here--";

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

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

passport.use(new TwitterStrategy({
    consumerKey: TWITTER_CONSUMER_KEY,
    consumerSecret: TWITTER_CONSUMER_SECRET,
    callbackURL: "http://127.0.0.1:3000/auth/twitter/callback"
  },
  function(token, tokenSecret, profile, done) {
    // asynchronous verification, for effect...
    process.nextTick(function () {

      return done(null, profile);
    });
  }
));


var app = express.createServer();

// configure Express
app.configure(function() {
  app.set('views', __dirname + '/views');
  app.set('view engine', 'ejs');
  app.use(express.logger());
  app.use(express.cookieParser());
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.session({ secret: 'keyboard cat' }));
  app.use(passport.initialize());
  app.use(passport.session());
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
});


app.get('/', function(req, res){
  res.render('index', { user: req.user });
});

app.get('/account', ensureAuthenticated, function(req, res){
  res.render('account', { user: req.user });
});

app.get('/login', function(req, res){
  res.render('login', { user: req.user });
});

app.get('/auth/twitter',
  passport.authenticate('twitter'),
  function(req, res){
    // The request will be redirected to Twitter for authentication, so this
    // function will not be called.
  });

app.get('/auth/twitter/callback', 
  passport.authenticate('twitter', { failureRedirect: '/login' }),
  function(req, res) {
    res.redirect('/');
  });

app.get('/logout', function(req, res){
  req.logout();
  res.redirect('/');
});

app.listen(3000);

function ensureAuthenticated(req, res, next) {
  if (req.isAuthenticated()) { return next(); }
  res.redirect('/login')
} 

How can I resolve this issue?

解决方案

I ran into a similar issue trying to get passport-oauth2 working. The error message, as you've observed, is less than helpful:

InternalOAuthError: Failed to obtain access token
    at OAuth2Strategy._createOAuthError (node_modules/passport-oauth2/lib/strategy.js:382:17)
    at node_modules/passport-oauth2/lib/strategy.js:168:36
    at node_modules/oauth/lib/oauth2.js:191:18
    at ClientRequest.<anonymous> (node_modules/oauth/lib/oauth2.js:162:5)
    at emitOne (events.js:116:13)
    at ClientRequest.emit (events.js:211:7)
    at TLSSocket.socketErrorListener (_http_client.js:387:9)
    at emitOne (events.js:116:13)
    at TLSSocket.emit (events.js:211:7)
    at emitErrorNT (internal/streams/destroy.js:64:8)

I found a suggestion to make a small change to passport-oauth2:

--- a/lib/strategy.js
+++ b/lib/strategy.js
@@ -163,7 +163,10 @@ OAuth2Strategy.prototype.authenticate = function(req, options) {

    self._oauth2.getOAuthAccessToken(code, params,
        function(err, accessToken, refreshToken, params) {
-          if (err) { return self.error(self._createOAuthError('Failed to obtain access token', err)); }
+          if (err) {
+            console.warn("Failed to obtain access token: ", err);
+            return self.error(self._createOAuthError('Failed to obtain access token', err));
+          }

Once I did that, I got a much more helpful error message:

Failed to obtain access token:  { Error: self signed certificate
    at TLSSocket.<anonymous> (_tls_wrap.js:1103:38)
    at emitNone (events.js:106:13)
    at TLSSocket.emit (events.js:208:7)
    at TLSSocket._finishInit (_tls_wrap.js:637:8)
    at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:467:38) code: 'DEPTH_ZERO_SELF_SIGNED_CERT' }

In my case I believe the root cause was that the authorization server I was testing with was using a self-signed SSL cert, which I was able to work around by adding this line:

require('https').globalAgent.options.rejectUnauthorized = false;

这篇关于InternalOAuthError:无法获取访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 21:08