获取缺少的必需参数:scope Passport.js,express
Getting Missing required parameter: scope Passport.js, express
我在我的应用程序中使用 Passport.js 和 express 来使用 Google Oauth 登录。但是当我尝试登录时,我收到以下错误:invalid parameter value for redirect_uri: Missing authority: http:localhost:3000/google/callback
,当我访问 localhost:3000/google/callback
时,我收到 Missing required parameter: scope
。相关代码:
const express = require("express");
const cors = require("cors");
const mongoose = require("mongoose");
const passport = require("passport");
const app = express();
const port = process.env.PORT || 3000;
require("dotenv").config();
require("./passport-setup")
app.use(passport.initialize())
app.use(passport.session())
app.get('/success', (req, res) => {
res.render("/profile.html")
})
app.get('/login', passport.authenticate('google', { scope: 'email' }));
app.get('/google/callback', passport.authenticate('google', { failureRedirect: '/failed' }),
function(req, res) {
// Successful authentication, redirect home.
res.redirect('/success');
}
);
护照配置(相关代码):
const passport = require("passport");
const GoogleStrategy = require("passport-google-oauth2").Strategy
passport.use(new GoogleStrategy({
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: process.env.CALLBACK_URL,
passReqToCallback: true
},function(request,accessToken,refreshToken,profile,done){
console.log(profile)
return done(null, profile)
}
))
PS:我找到了,但我不知道他所说的'JSON key'是什么意思。也许 API 已更新。
任何帮助将不胜感激。
提前致谢。
编辑
我提供给 google 的回调 url 与我的“app.get”不匹配。固定的。
我的回电 URL 是 http:localhost:3000/google/callback
将其设置为 /gooogle/callback
对我有用。
PS: I found this answer but i don't know what he means by 'JSON
key' - the person referred to your GoogleStrategy
which is A JSON
object contains zero, one, or more key-value pairs.
/* SECURE COOKIES TRUE MADE GOOGLE 登录/注册工作 */
我在尝试使用 Google 帐户签署 in/up 时遇到错误:Missing required parameter: scope
并且在 之前添加了以下代码行app.use(passport.initialize());
成功了!
if (app.get("env") === "production") {
app.set("trust proxy", 1);
session.cookie.secure = true;
}
您可能必须实施 express-session
才能完成此工作(查看 npm 网站以获取有关如何使用的文档)但只需在顶部声明:
const session = require("express-session");
在 app.use(passport.initialize())
之前添加这行:
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
cookie: {},
})
);
我在我的应用程序中使用 Passport.js 和 express 来使用 Google Oauth 登录。但是当我尝试登录时,我收到以下错误:invalid parameter value for redirect_uri: Missing authority: http:localhost:3000/google/callback
,当我访问 localhost:3000/google/callback
时,我收到 Missing required parameter: scope
。相关代码:
const express = require("express");
const cors = require("cors");
const mongoose = require("mongoose");
const passport = require("passport");
const app = express();
const port = process.env.PORT || 3000;
require("dotenv").config();
require("./passport-setup")
app.use(passport.initialize())
app.use(passport.session())
app.get('/success', (req, res) => {
res.render("/profile.html")
})
app.get('/login', passport.authenticate('google', { scope: 'email' }));
app.get('/google/callback', passport.authenticate('google', { failureRedirect: '/failed' }),
function(req, res) {
// Successful authentication, redirect home.
res.redirect('/success');
}
);
护照配置(相关代码):
const passport = require("passport");
const GoogleStrategy = require("passport-google-oauth2").Strategy
passport.use(new GoogleStrategy({
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: process.env.CALLBACK_URL,
passReqToCallback: true
},function(request,accessToken,refreshToken,profile,done){
console.log(profile)
return done(null, profile)
}
))
PS:我找到了
任何帮助将不胜感激。 提前致谢。
编辑
我提供给 google 的回调 url 与我的“app.get”不匹配。固定的。我的回电 URL 是 http:localhost:3000/google/callback
将其设置为 /gooogle/callback
对我有用。
PS: I found this answer but i don't know what he means by 'JSON key' - the person referred to your
GoogleStrategy
which is A JSON object contains zero, one, or more key-value pairs.
/* SECURE COOKIES TRUE MADE GOOGLE 登录/注册工作 */
我在尝试使用 Google 帐户签署 in/up 时遇到错误:Missing required parameter: scope
并且在 之前添加了以下代码行app.use(passport.initialize());
成功了!
if (app.get("env") === "production") {
app.set("trust proxy", 1);
session.cookie.secure = true;
}
您可能必须实施 express-session
才能完成此工作(查看 npm 网站以获取有关如何使用的文档)但只需在顶部声明:
const session = require("express-session");
在 app.use(passport.initialize())
之前添加这行:
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
cookie: {},
})
);