【问题标题】:Grails Spring Security Core - How to display profile picture using Spring Security Facebook plugin?Grails Spring Security Core - 如何使用 Spring Security Facebook 插件显示个人资料图片?
【发布时间】:2015-02-16 19:30:42
【问题描述】:

我正在构建我的第一个 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
}

【问题讨论】:

  • 这是 Spring Social Facebook 的工作,我记得那很棘手,但找不到确切的代码
  • @Griffin,只是想知道您是否找到了解决此问题的方法。如果是,请发帖。谢谢。

标签: facebook spring grails grails-plugin spring-social


【解决方案1】:

在我的情况下,我需要 imageUrl 而不是图像本身

您可以使用以下网址获取不同大小的个人资料图片。请确保将 Facebook id 添加到 url。

大尺寸照片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

附:您可以从 userProfile 获取 facebookId: facebook.userOperations().getUserProfile();

【讨论】:

    【解决方案2】:

    我得到字节数组并将其保存为图片。

    byte[] profilePic = facebook.userOperations().getUserProfileImage();

    【讨论】:

      猜你喜欢
      • 2016-03-23
      • 2012-09-29
      • 2019-05-09
      • 2012-08-06
      • 2012-11-04
      • 2015-06-18
      • 1970-01-01
      • 2018-08-01
      • 2016-05-24
      相关资源
      最近更新 更多