当护照不返回profile.emails, profile.name.givenName, profile.name.familyName
字段时,或者如果它们丢失,您可以尝试解析https://graph.facebook.com/v3.2/
url,尽管您仍然需要一个令牌。您当然可以使用有效的令牌访问 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"
}
安装请求模块 ( $ 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; // body.email contains your email
console.log(body);
});
}
));
您可以向 url 添加许多其他参数,尽管其中一些需要用户权限才能返回值。你可以玩它:https ://developers.facebook.com/tools/explorer/