facebook - Grails Spring Security Core-如何使用Spring Security Facebook插件显示个人资料图片?

标签 facebook spring grails grails-plugin spring-social

我正在构建我的第一个grails应用程序,并且已经成功集成了以下所有插件:

  • Spring Security Core plugin
  • Facebook Authentication for Spring Security
  • Spring Security UI plugin

  • 当前,我可以单击“使用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/

    相关文章:

    grails - Grails 1.3.7如何在不进行任何缓冲的情况下直接写入输出流

    Facebook iframe 选项卡签名请求始终为空

    java - 如何在我的 Spring Boot 代码中处理亚马逊 s3 存储桶的 404 错误

    java - @ExceptionHandler for type Exception not called for Spring exception in custom ResponseEntityExceptionHandler

    git - Grails .gitignore 的建议

    grails - Grails DBMigrations插件的用法困惑

    android - React Native 链接打开 Facebook Messenger

    ios - 如何发布到用户的 Facebook 提要

    ios - 不允许使用读取权限请求发布或管理权限

    java - 从 Elasticseatch 2.x 升级到 5.x