[英]Failed to fetch user profile login via google with node and passport
I am trying to login via google using node and package passport-google-oauth20.我正在尝试使用节点和包passport-google-oauth20通过谷歌登录。 But getting the error - "InternalOAuthError: Failed to fetch user profile".
但收到错误 - “InternalOAuthError:无法获取用户配置文件”。 Already enable google plus api.
已经启用google plus api。 I am using my business gmail id to access this.
我正在使用我的企业 gmail id 来访问它。 Everything is stored in mongodb but not getting the return json as a response.
一切都存储在 mongodb 中,但没有得到返回 json 作为响应。 Here is my passport-setup.js code.
这是我的passport-setup.js 代码。
const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20');
const keys = require('./keys');
const User = require('../api/models/user');
const mongoose = require('mongoose');
passport.use(
new GoogleStrategy({
callbackURL: '/users/google/redirect',
clientID: keys.google.clientID,
clientSecret: keys.google.clientSecret
}, (accessToken, refreshToken, profile, done)=>
{
User.findOne({googleid : profile.id})
.exec()
.then(doc=>{
if(doc)
{
//already exist user
console.log("User exist : "+doc)
} else{
const user = new User({
_id : new mongoose.Types.ObjectId(),
name : profile.displayName,
googleid : profile.id
});
user.
save().
then(result=>{
console.log("myerror1" +result);
})
}
})
.catch(err=>{
console.log("error1" + err);
});
console.log(accessToken);
console.log(profile);
})
)
And here is routes code这是路线代码
router.get('/google', passport.authenticate('google', {
scope: ['profile']
}));
router.get('/google/redirect', passport.authenticate('google'), (req, res)=>
{
res.send(req.user);
});
I am gettting error on google.redirect request.我在 google.redirect 请求中遇到错误。
req.user
请求用户
showing me InternalOAuthError向我展示 InternalOAuthError
Have you checked this documentation from google?你有没有从谷歌检查过这个文档? It may help.
它可能会有所帮助。
Also, can you add .Strategy
at the end of const GoogleStrategy = require('passport-google-oauth20');
另外,您可以在
const GoogleStrategy = require('passport-google-oauth20');
的末尾添加.Strategy
const GoogleStrategy = require('passport-google-oauth20');
and see if it helps (or if you get a different error message?看看它是否有帮助(或者你是否收到不同的错误信息?
const GoogleStrategy = require('passport-google-oauth20').Strategy;
I faced the same problem and I solved it doing this:我遇到了同样的问题,我解决了这个问题:
In package.json, change "passport" value to "^0.4.0" and "passport-google-oauth" to "^2.0.0".在 package.json 中,将“passport”值更改为“^0.4.0”,将“passport-google-oauth”更改为“^2.0.0”。 Run "npm install" again.
再次运行“npm install”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.