NodeJS 如何使用passport.js github策略获取用户的邮件而不是null

jhkqcmku  于 2022-11-29  发布在  Node.js
关注(0)|答案(1)|浏览(78)

I successfully got the user details but the email field is null.After some time got to know that I need to fetch it from "https://api.github.com/user/emails".I used axios and also provided accesstoken as header but it is giving me "▼♥��A�@►���\�%♥+=y��5"V↔ubWevW�迧F�¶◄t��☻�%)H�ɢ�k��♥葩$,�7↓�H�↔?��^Z�k�r���:��x�▬♣▬NP������҇�C�v�C▼o.�pK~☺" instead of emails.

passport.use(new GitHubStrategy({
    clientID: process.env.GITHUB_CLIENT_ID,
    clientSecret: process.env.GITHUB_CLIENT_SECRET,
    callbackURL: "/callbackurl"
  },
  function(accessToken, refreshToken, profile, done) {
  User.findOrCreate({ githubId: profile.id }, function (err, user){   //I am using mongoose-findorcreate npm
      axios.get('https://api.github.com/user/emails', {
 headers: {
   "Authorization": "Bearer "+ accessToken,
 }
}).then((res)=>{
  console.log(res.data);
})
      return done(err, user);
    });
  }
));
z2acfund

z2acfund1#

这是因为你还没有在你的策略中指定范围,所以在你的github策略中,也包括范围关键字:作用域:[“用户:电子邮件”],身份:

`new GitHubStrategy({
    clientID: process.env.GITHUB_CLIENT_ID,
    clientSecret: process.env.GITHUB_CLIENT_SECRET,
    callbackURL: "/callbackurl",
    scope: ["user:email"]
}`

相关问题