[英]Passport failing with 404 - InternalOAuthError for GitHub why?
I have passport working with Google, and Facebook. 我有护照与谷歌和Facebook合作。 I have attempted to add Github to add these credentials so I can do validated Github API calls. 我试图添加Github来添加这些凭据,以便我可以进行有效的Github API调用。 So I simply added the same pattern I used to login using either Google or Facebook credentials. 所以我只是使用Google或Facebook凭据添加了我用于登录的相同模式。
BUT I see InternalOAuthError in the middle of my code after the auth callback has taken place from Github. 但是在我从Github进行auth回调之后,我在代码中间看到了InternalOAuthError。 This happens when the nearly last line: 'return done(null, user.userData);' 这几乎发生在最后一行:'return done(null,user.userData);' is called. 叫做。 Attempting to debug interferes with the callbacks. 尝试调试会干扰回调。 SO I am hoping that someone with greater clarity about passport can explain what I am doing wrong. 因此,我希望有一个关于护照更清晰的人可以解释我做错了什么。
What is really strange is that I have already received the user profile from github and stored in in my database with 'user.update(db)', in the same way I do with google. 真正奇怪的是,我已经从github收到了用户个人资料,并以'user.update(db)'存储在我的数据库中,就像我使用google一样。 Then the crash happens when I attempt to return by calling done(...). 然后当我尝试通过调用done(...)返回时发生崩溃。
Do I need to add something to my profile on github? 我是否需要在github上添加一些内容到我的个人资料中? or something else? 或者是其他东西? Or is this because I already have used passport much earlier to login using Google credentials. 或者这是因为我早已使用过很久的护照来使用Google凭据登录。 Note that for Google, or Facebook, I have specified session: false. 请注意,对于Google或Facebook,我指定了session:false。 I have attempted this with both 'passport-github' and 'passport-github2'. 我用'passport-github'和'passport-github2'试图这样做。
The code for clarity is: 清晰的代码是:
index.js index.js
var express = require('express');
var passport = require('passport');
var auth = require('../auth.service');
var router = express.Router();
router
.get('/:user', passport.authenticate('github', {
failureRedirect: '/signup',
session: false
}))
.get('/callback', passport.authenticate('github', {
failureRedirect: '/signup',
session: true
}), auth.setTokenCookie);
module.exports = router;
and the corresponding passport.js 和相应的passport.js
var passport = require('passport');
var GitHubStrategy = require('passport-github2').Strategy;
var monk_db = rekuire('server/monk_db');
var loggingAndErrors = rekuire('./loggingAndErrors');
var auth = require('../auth.service');
var config = require('../../config/environment');
var jwt = require('jsonwebtoken');
var expressJwt = require('express-jwt');
var validateJwt = expressJwt({ secret: config.secrets.session });
var jwtDecode = require('jwt-decode');
var ObjectID = require("bson-objectid");
exports.setup = function (User, config) {
passport.use(new GitHubStrategy({
clientID: config.github.clientID,
clientSecret: config.github.clientSecret,
callbackURL: config.github.callbackURL,
passReqToCallback: true
},
function(req, accessToken, refreshToken, profile, done) {
//loggingAndErrors.logger.log(accessToken);
var token = req.cookies.token;
var decoded = jwtDecode(token);
var user_id = decoded._id;
var db = monk_db.getDb();
var users = User.getUsers(db);
users.findById(ObjectID(user_id), function(err, user) {
if (err) {
loggingAndErrors.loggerError.log(err);
return done(err);
}
//loggingAndErrors.logger.log(profile);
user.github=profile._json
user = User.newUser(user);
user.update(db).onResolve(function(err, result) {
if (err) {
loggingAndErrors.loggerError.log(err);
return done(err);
}
//loggingAndErrors.logger.log(user);
loggingAndErrors.logger.log("calling done(err, user) for user_id:", user.userData._id);
return done(null, user.userData);
});
});
}
));
};
The crash is: 崩溃是:
{ statusCode: 404,
data: '{"message":"Not\
Found","documentation_url":"https://developer.github.com/v3"}' }
GET /auth/github/callback?code=7c0c6dff81cdd9417301 500 945.444 ms - 674
InternalOAuthError: Failed to fetch user profile
at /home/joel/workspace/Tracker2/node_modules/passport-github2/lib/strategy.js:118:21
at passBackControl (/home/joel/workspace/Tracker2/node_modules/passport-github2/node_modules/passport-oauth2/node_modules/oauth/lib/oauth2.js:123:9)
at IncomingMessage.<anonymous> (/home/joel/workspace/Tracker2/node_modules/passport-github2/node_modules/passport-oauth2/node_modules/oauth/lib/oauth2.js:142:7)
at IncomingMessage.emit (events.js:129:20)
at _stream_readable.js:908:16
at process._tickDomainCallback (node.js:381:11)
I just ran into the same problem, so here is what works for me. 我刚遇到同样的问题,所以这里对我有用。
Use passport-github2 and request access to the user's email address when authenticating: 使用passport-github2并在验证时请求访问用户的电子邮件地址:
passport.authenticate('github', { scope: [ 'user:email' ] }));
There are other permissions that you may want to access at the same listed in the GitHub OAuth documentation . 您可能希望访问GitHub OAuth文档中列出的其他权限。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.