我有一个项目,这个项目有一个登录页面,我使用护照js,google-ouath20 20。
我需要性别,生日信息,这就是为什么我在google上增加了新的范围
router.get(
"/login/google",
passport.authenticate("google", { scope: [ "https://www.googleapis.com/auth/user.gender.read", "https://www.googleapis.com/auth/user.birthday.read", "https://www.googleapis.com/auth/user.addresses.read", "profile", "email" ] })
);
但毕竟,当我试图获取信息时,我只需到达个人资料和电子邮件信息。这其中哪一部分我搞错了?
passport.use(
new GoogleStrategy(
{
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: GOOGLE_CALLBACK_URL,
passReqToCallback: true,
},
async (req, accessToken, refreshToken, profile, cb) => {
console.log("profile: ",profile) ...
发布于 2022-04-13 07:56:11
您可以使用如文档链接中所描述的人员api来获取此类数据。
从people API中获取数据需要一个有效的API密钥2。
id)?personFields=(字段)您想要的)&key=(有效的api密钥)&access_token=(在认证结果中的accessToken)
链接 2.在此处添加字段名?personFields=(fields you want)
3。如果最终用户想让他们的数据可供应用程序使用,则必须复选框。
我用来获取用户生日的代码
router.get(
"/login/google",
passport.authenticate("google")
);
// strategy code
const getbirthDay = async (id, accessToken) => {
const response = await fetch(`https://people.googleapis.com/v1/people/${id}?personFields=birthdays&key=${process.env.GOOGLE_PEOPLE_API_KEY}&access_token=${accessToken}`);
data.birthdays.map(entry => {
const { year, month, day } = entry.date
console.log(JSON.stringify(entry.date, undefined, 4));
});
// const { year, month, day } = data.birthdays[0].date
}
passport.use(
new GoogleStrategy(
{
clientID: GOOGLE_CLIENT_ID,
clientSecret: GOOGLE_CLIENT_SECRET,
callbackURL: GOOGLE_CALLBACK_URL,
passReqToCallback: true,
scope: ['profile', 'email', 'https://www.googleapis.com/auth/user.birthday.read'],
state: true
},
async (req, accessToken, refreshToken, profile, cb) => {
const { id } = profile._json
await getbirthDay(id, accessToken)
}
))
https://stackoverflow.com/questions/69324453
复制