我使用MEAN框架开发了一个应用程序,并使用passportjs的google策略进行身份验证。本地运行运行良好,但当我将其部署到 Heroku 时,因为 Heroku 在随机端口上运行其应用程序。我不确定需要在 Google 控制台的“授权重定向 URI”中添加什么 Google 回调 URL。
passport.use(new GoogleStrategy({
clientID: config.googleAuth.clientID,
clientSecret: config.googleAuth.clientSecret,
callbackURL: config.googleAuth.callbackURL
}, function (token, refreshToken, profile, done) {
console.log(token, refreshToken, profile, done);
var query = {
'google.id' : profile.id
};
User.findOne(query, function (err, user) {
if(user){
console.log("User found in the database");
done(null, user);
}
else{
var newUser = new User;
newUser.displayName = profile.displayName;
newUser.image = profile.photos[0].value;
newUser.google = {};
newUser.google.id = profile.id;
newUser.google.token = token;
newUser.save();
console.log("saved user to the database");
done(null, newUser);
}
});
}));
上面显示的代码是我的谷歌策略。我正在使用 Passport-google-oauth lib 进行身份验证。
module.exports = {
development: {
rootPath: rootPath,
db: 'xxx',
port: process.env.PORT || 3030,
googleAuth: {
clientID: 'xxx',
clientSecret: 'xxx',
callbackURL: 'http://localhost:3030/auth/google/callback'
}
},
production: {
rootPath: rootPath,
db: 'xxx',
port: process.env.PORT || 80,
googleAuth: {
clientID: 'xxx',
clientSecret: 'xxxx',
callbackURL: 'https://<myheroku-app>:<heroku-port-no>/auth/google/callback'
}
}
}
以上是我的Google策略的详细内容。如果我添加 http://localhost:3030/auth/google/callback 本地主机部分工作正常到授权重定向 URI。但是,当我尝试对 heroku 应用程序执行相同操作时,我收到 400 服务器错误,错误为 Error:redirect_uri_mismatch。
我该如何解决这个问题?我非常接近部署这个应用程序并且坚持使用这个东西。如果您需要更多信息,请告诉我。
最佳答案
关于node.js - 在 Heroku 中部署时,Google 回调 url 返回 400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42919489/