Facebook에서 Facebook을 사용하여 Facebook에서 사진을 검색하기
-
21-12-2019 - |
문제
Passport-Facebook을 통해 기본 사용자 정보를 검색하고 아래 코드를 따라 MongoDB에서 저장합니다.
app.get("/auth/facebook", passport.authenticate("facebook", { scope : ["email", "publish_stream", "user_location", "user_hometown", "user_birthday", "read_friendlists"]}));
app.get("/auth/facebook/callback", passport.authenticate("facebook",{ successRedirect: '/', failureRedirect: '/'}));
var mongoose = require('mongoose'),
FacebookStrategy = require('passport-facebook').Strategy,
Users = mongoose.model('Users');
module.exports = function (passport, config) {
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
Users.findOne({ _id: id }, function (err, user) {
done(err, user);
});
});
passport.use(new FacebookStrategy({
clientID: config.facebook.clientID,
clientSecret: config.facebook.clientSecret,
callbackURL: config.facebook.callbackURL
}, function(accessToken, refreshToken, profile, done) {
Users.findOrCreateFaceBookUser(profile, done);
}));};
.
그러나 "프로필"에서 프로필 사진을 볼 수 없습니다.
문서는 https://github.com/jaredhanson/passport-facebook 우리가 전달해야 할 사진을 검색하기 위해아래와 같은 프로필 필드.그러나 그렇게하면 사진 URL을 볼 수 있지만 _json 예를 들어 포함 된 다른 데이터를 잃을 수 있습니다.profile._json.location.name.다른 사용자 정보로 사진을 그대로 검색 할 수 있습니까?
passport.use(new FacebookStrategy({
// clientID, clientSecret and callbackURL
profileFields: ['id', 'displayName', 'photos', ....]},// verify callback));
. 해결책 3
Yes, the picture can be accessed via the graph api using the access token like this. "graph.facebook.com/"; + profile.username + "/picture" + "?width=200&height=200" + "&access_token=" + accessToken; There is no need to use the profile fields.
다른 팁
큰 이미지가 필요하면 위의 miksii의 예제는 꽤 작은 50px x 50px입니다.) 다음을 사용하십시오.
profileFields: ['id', 'displayName', 'name', 'gender', 'picture.type(large)']
.
및 그 다음
picture: profile.photos ? profile.photos[0].value : '/img/faces/unknown-user-pic.jpg'
.
해당 사용자에 대해 200px x 200px 프로파일 그림을 반환합니다.
In addition to answer of your question - you don't have to do it that way. As you mentioned you can define the required attributes for Facebook profile:
clientID: "...",
clientSecret: "...",
callbackURL: "...",
profileFields: ['id', 'displayName', 'name', 'gender', ..., 'photos']
What than you can do is just simply grab the value of the given attribute. Let's say you want to make an attribute that will hold this value:
picture: profile.photos ? profile.photos[0].value : '/img/faces/unknown-user-pic.jpg'
This proved to be a better solution since some users or sometimes the value of username may be undefined.
I hope you find this useful too,
Thank you.
As this answer, it will be work better with this code.
passport.use(new FacebookStrategy({
clientID: FACEBOOK_APP_ID,
clientSecret: FACEBOOK_APP_SECRET,
callbackURL: FACEBOOK_APP_CALLBACK,
profileFields: ['id', 'displayName', 'picture.type(large)', 'email', 'birthday', 'friends', 'first_name', 'last_name', 'middle_name', 'gender', 'link']
}, (accessToken, refreshToken, profile, cb) => {
const picture = `https://graph.facebook.com/${profile.id}/picture?width=200&height=200&access_token=${accessToken}`
//
}))