[英]Google callback url giving back 400 when deployed in heroku
I developed an app using MEAN framework and used passportjs's google strategy to authenticate.我使用 MEAN 框架开发了一个应用程序,并使用了passportjs 的谷歌策略进行身份验证。 The local run runs fine, but when I deploy the same to heroku, since Heroku runs its app on a random port.本地运行运行良好,但是当我将其部署到 heroku 时,因为 Heroku 在随机端口上运行其应用程序。 I am not sure what google callback url I need to add in my google console's "Authorized redirect URIs".我不确定需要在我的 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);
}
});
}));
The above shown code is my google strategy.上面显示的代码是我的谷歌策略。 I am using passport-google-oauth lib for authentication.我正在使用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'
}
}
}
The above is the details of my google strategy.以上就是我的google策略的详细介绍。 The localhost part works fine if I add http://localhost:3030/auth/google/callback to the Authorized redirect URI's.如果我将http://localhost:3030/auth/google/callback 添加到授权重定向 URI,则 localhost 部分工作正常。 But when I try to do the same for the heroku app, I get a 400 server error with Error: redirect_uri_mismatch as the error.但是,当我尝试对 heroku 应用程序执行相同操作时,出现 400 服务器错误,错误为 Error: redirect_uri_mismatch。
How do I fix this thing?我该如何解决这个问题? I am so close to deploying this app and stuck with just this thing.我非常接近部署这个应用程序并坚持使用这个东西。 Let me know if you would need any more info.如果您需要更多信息,请告诉我。
You could use relative paths at callbackURL (eg callbackURL: '/auth/google/callback') to make your code less depended on specific domains, and add an additional element on googleAuth, by putting a comma after callbackURL, as:您可以在 callbackURL 处使用相对路径(例如 callbackURL: '/auth/google/callback'),使您的代码不那么依赖于特定域,并在 googleAuth 上添加一个附加元素,方法是在 callbackURL 后放置一个逗号,如下所示:
proxy: true
After this, you can use https on google's developers console for your callbacks.在此之后,您可以在 google 的开发人员控制台上使用 https 进行回调。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.