node.js - 使用 passport-google-oauth20 时无法访问重定向 URL
问题描述
我刚开始学习 OAuth。我正在将passport-google-oauth20 与node.js 一起使用。下面是我项目的 passport-setup.js 文件中的代码。
const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20');
const keys = require('./keys')
passport.use(
new GoogleStrategy({
//options for google strategy
callbackURL:'https://localhost:3000/auth/google/redirect',
clientID:keys.google.clientID,
clientSecret:keys.google.clientSecret
}, function(){
//passport callback function
})
);
我项目的 auth-route.js 文件中的代码
const router = require('express').Router();
const passport = require('passport');
//auth login
router.get('/login', function(req,res){
res.render('login');
});
//auth logout
router.get('/logout', function(req,res){
//have to handle with passport
res.send('Logging out');
});
//auth with google
router.get('/google', passport.authenticate('google',{
scope:['profile']
}));
//callback route for google
router.get('/google/redirect', passport.authenticate('google'),
function(req,res){
res.send('You logged in');
});
module.exports = router;
可以参考完整代码@https ://github.com/Ayush-Porwal/OAuth-learning.git
我对重定向 URL 有问题。当 callbackURL 持有“ https://localhost:3000/auth/google/redirect ”的值时,浏览器会将我定向到 OAuth 同意屏幕。但是,当我尝试登录后,浏览器(chrome)会给我一个错误/消息,如屏幕截图所示。
而在 Firefox 上,消息如图所示(添加只是因为错误消息不同)
当 callbackURL 保持“/auth/google/redirect”的值时(我认为这两个值应该给出相同的结果)。浏览器给出的错误为
这是我在谷歌开发者网站上的应用程序中输入的 URI 的屏幕截图。
我尝试了几件事,例如稍微更改代码等,但我无法意识到问题出在哪里。请帮忙。
解决方案
过程不完整。我现在已经完成了我的项目,并且我已经写了一个相同的答案。请从这里跟进我在Google Oauth 上给出代码兑换错误的答案
推荐阅读
- javascript - 如何生成数字,以便永远没有背靠背的数组索引值?
- selenium - Selenium 脚本中的 JavascriptExecutor 抛出“org.openqa.selenium.JavascriptException: javascript error”
- javascript - 将字符串中的所有字符转换为小于 256 的值的十六进制格式
- bash - 为什么我不能使用 cal 命令在 Git Bash 中显示日历?
- c# - .Net Core T4 Running transformation could not load file or assembly
- html - Flask Fonts not Updating
- virtualbox - qemu - 将虚拟机桥接到 Virtualbox 中的虚拟机
- python - django模型与rest api的关系
- windows - MiKTeX、LaTeX:使用 \newwrite 输出文件和使用 \write 写入时出错——Windows API 错误 2:系统找不到指定的文件
- google-apps-script - 从 Google Geocode API 获取有效住宅地址的零结果