2

I am building my first grails app and have successfully integrated all of the following plugins:

Currently, I can click a "Login with Facebook" button and I've added a FacebookAuthService which allows me to fetch username, e-mail, etc. and I am displaying the username to the screen using the "sec" taglib. My question is regarding how to fetch the user's Facebook Profile Picture and display it to the page. I've looked all over and couldn't find a good example. Can anyone provide one?

I have the following method defined in 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
}
Igor Artamonov
  • 35,450
  • 10
  • 82
  • 113
Griffin
  • 133
  • 1
  • 7

2 Answers2

1

In my case I needed imageUrl but not image itself

You can use following urls to obtain different sizes of profile images. Please make sure to add Facebook id to url.

Large size photo https://graph.facebook.com/{facebookId}/picture?type=large

Medium size photo https://graph.facebook.com/{facebookId}/picture?type=normal

Small size photo https://graph.facebook.com/{facebookId}/picture?type=small

Square photo https://graph.facebook.com/{facebookId}/picture?type=square

P.s. You can get facebookId from userProfile: facebook.userOperations().getUserProfile();

bmelnychuk
  • 1,100
  • 8
  • 14
0

I got byte array and saved this as pic.

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

Rahul
  • 31
  • 1
  • 5