Google OpenID回调未触发:是否为localhost导致的问题?
问题描述
按照Google OpenID文档实现认证请求,发送到https://accounts.google.com/o/oauth2/v2/auth后,既没有重定向到我的redirect_uri(http://localhost:5901),也没出现Google登录界面。已确认CLIENT_ID正确,想问是不是localhost的问题?

认证逻辑代码
const URL = 'https://accounts.google.com/o/oauth2/v2/auth'; // 初始化登录;重定向获取权限 const constructCodeURI = () => { const queryString = `client_id=${CLIENT_ID}` + `&scope=openidemail` + '&state=019146662993491234567890' + '&login_hint=foo.bar@gmail.com' + '&nonce=03852-31485-24958' + `&redirect_uri=http://${HOST}/auth/google/callback` + '&response_type=code'; return `${URL}?${queryString}`; }; // 用户授权后获取授权码 const redirectToGoogleLogin = (req, res) => { const url = constructCodeURI(); console.log('aaa'); console.log(url); res.redirect(url); }; const callbackStack = (req, res) => { console.log('This is not being hit'); if (Object.keys(req.query).length > 0) { getTokenAndProfileStack(req, res, code, 'google') .then(e => res.redirect('http://localhost:5000/forms/ui/list_forms')) .catch(e => console.log('google login call back err ' + e)) } }; export default callbackStack;
路由配置代码
// google login app.route('/auth/google/login') .get(redirectToGoogleLogin); app.route('/auth/google/callback') .get(callbackStack);
实际发送的请求(已脱敏)
https://accounts.google.com/o/oauth2/v2/auth?client_id=20oogleusercontent.com&scope=openidemail&state=034912340&login_hint=foo.bar@gmail.com&nonce=02-31905-8&redirect_uri=http://127.0.0.1:5901/auth/google/callback&response_type=code
排查方案
修复scope参数格式错误
你的请求里scope=openidemail是无效格式,多个scope必须用空格分隔,转义后会自动变成openid%20email。代码里要改成:+ `&scope=openid email`这个错误会直接导致Google认证请求失效,是当前最可能的问题。
确保redirect_uri完全一致
实际请求用的是http://127.0.0.1:5901/auth/google/callback,要和Google开发者控制台里「已获授权的重定向URI」列表里的地址完全匹配,包括用localhost还是127.0.0.1、端口、路径都不能有差异。临时移除login_hint测试
如果login_hint对应的邮箱已在当前浏览器登录,Google可能尝试静默授权,但如果该账号未给你的应用授权,会出现无响应情况。先去掉这个参数,看是否能正常显示登录界面。查看浏览器开发者工具
打开浏览器控制台和网络面板,检查发送到Google的请求状态码、返回内容,以及是否有JS报错,这能快速定位具体问题。
内容的提问来源于stack exchange,提问作者dman
相关产品推荐
相关产品推荐

