当护照没有返回
profile.emails,profile.name.givenName,profile.name.familyName
字段或者它们缺失时,您可以尝试解析
https://graph.facebook.com/v3.2/
url,但您仍然需要一个token。您可以使用有效的token访问url,例如:
https://graph.facebook.com/v3.2/me?fields=id,name,email,first_name,last_name&access_token=
它会输出一个JSON响应,如下:
{
"id": "5623154876271033",
"name": "Kurt Van den Branden",
"email": "kurt.vdb\u0040example.com",
"first_name": "Kurt",
"last_name": "Van den Branden"
}
安装 request 模块 ($ npm install request --save
),用于解析 JSON url,并在您的 passport.js 文件中:
const request = require("request");
passport.use(new FacebookStrategy({
clientID : 'CLIENT_ID',
clientSecret : 'CLIENT_SECRET',
callbackURL : "https://example.com/auth/facebook/callback"
},
function(req, token, profile, done) {
let url = "https://graph.facebook.com/v3.2/me?" +
"fields=id,name,email,first_name,last_name&access_token=" + token;
request({
url: url,
json: true
}, function (err, response, body) {
let email = body.email;
console.log(body);
});
}
));
你可以在url中添加很多其他参数,但其中一些需要用户允许才能返回值。你可以在以下网址进行尝试:https://developers.facebook.com/tools/explorer/
accessToken
并使用Graph API Explorer进行尝试。确保左侧的“电子邮件”权限未变灰。如果是,您没有在正确的时间请求范围。请参见passport-facebook项目上的此评论。 - a paid nerd