我正在构建我的第一个grails应用程序,并且已经成功集成了以下所有插件:
当前,我可以单击“使用Facebook登录”按钮,并添加了FacebookAuthService,该服务允许我获取用户名,电子邮件等,并且使用“sec”标签库在屏幕上显示用户名。我的问题是关于如何获取用户的Facebook个人资料图片并将其显示到页面上。我四处张望,找不到一个很好的例子。谁能提供一个?
我在FacebookAuthService中定义了以下方法:
FacebookUser create(FacebookAuthToken token) {
log.info("Create domain for facebook user $token.uid")
//Use Spring Social Facebook to load details for current user from Facebook API
Facebook facebook = new FacebookTemplate(token.accessToken.accessToken)
FacebookProfile fbProfile = facebook.userOperations().userProfile
String email = fbProfile.email
String username = fbProfile.username
String firstName = fbProfile.firstName
String lastName = fbProfile.lastName
User person = new User(
username: [firstName, lastName].join(' '),
password: token.accessToken.accessToken, //not really necessary
enabled: true,
accountExpired: false,
accountLocked: false,
passwordExpired: false,
//fill with data loaded from Facebook API
name: [firstName, lastName].join(' '),
email: email
)
person.save( flush : true)
person.validate()
println person.errors
UserRole.create(person, Role.findByAuthority('ROLE_USER'))
UserRole.create(person, Role.findByAuthority('ROLE_FACEBOOK'))
FacebookUser fbUser = new FacebookUser(
uid: token.uid,
accessToken: token.accessToken.accessToken,
accessTokenExpires: token.accessToken.expireAt,
user: person
)
fbUser.save()
return fbUser
}
最佳答案
就我而言,我需要imageUrl,但不需要图像本身
您可以使用以下网址获取不同大小的个人资料图像。请确保将Facebook ID添加到网址。
大型照片https://graph.facebook.com/ {facebookId} / picture?type = large
中型照片https://graph.facebook.com/ {facebookId} / picture?type = normal
小型照片https://graph.facebook.com/ {facebookId} / picture?type = small
方形照片https://graph.facebook.com/ {facebookId} / picture?type = square
P.s.您可以从userProfile获取facebookId:facebook.userOperations().getUserProfile();
关于facebook - Grails Spring Security Core-如何使用Spring Security Facebook插件显示个人资料图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27539340/