即使将{scope: ['user:email']}传递给passport.authenticate('github', {scope: ['user:email'] , session: false})(req, res, next),passport-github2也会在电子邮件字段中返回null
发布于 2020-11-06 12:50:12
在看了一下源代码后,我发现有一个函数可以获取用户的电子邮件,但为此,我们需要传递初始化GitHubStrategy的作用域。我不知道为什么没有任何地方记录它!为了让它启动并工作,我发现我们需要做以下事情:
passport.use(new GitHubStrategy({
clientID: GITHUB_OAUTH_CLIENT_ID,
clientSecret: GITHUB_OAUTH_CLIENT_SECRET,
callbackURL: GITHUB_OAUTH_CALLBACK,
proxy: true,
scope: ['user:email'] //This is all it takes to get emails
}, (accessToken, refreshToken, profile, next) => {
// You get the profile object with emails array which can be accessed with
// profile.emails[n].value
}));//middleware
passport.authenticate('github', {scope: ['user:email'] , session: false})(req, res, next)这真的很奇怪,它没有在他们的回购中记录。
注意:由于对于GitHub,如果用户将电子邮件地址设置为私有,则无法直接获取电子邮件地址,因此需要调用另一个端点(docs)。它写在这里的源代码中:(source)
PS:我仍然在寻找一种更好的方法来访问电子邮件,而不需要在两个地方通过范围。
https://stackoverflow.com/questions/64708917
复制相似问题