Search code examples
facebookgrailsspring-securityspring-socialspring-social-facebook

Grails facebook spring security returning null


I am using ":spring-security-facebook:0.17" integrated with ":spring-security-core:2.0-RC5". I have implemented facebookAuthService.groovy for customization. But the token is not authenticated and the fbProfile is returning null except id and name.

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
    FacebookProfile fbProfile2 = facebook.userOperations().getUserProfile()
    def ff = facebook.userOperations().userPermissions
    String email = fbProfile.email
    String username = fbProfile.username
    String firstName = fbProfile.firstName
    String lastName = fbProfile.lastName

    println fbProfile as JSON
    println token as JSON
    println fbProfile.email

    User person = new User(
            username: 'jgf',
            password: token.accessToken.accessToken, //not really necessary
            enabled: true,
            accountExpired:  false,
            accountLocked: false,
            passwordExpired: false,

            //fill with data loaded from Facebook API
            email: email
    )
    person.save(failOnError: true)
    UserRole.create(person, AuthRole.findByAuthority('ROLE_USER'))
    UserRole.create(person, AuthRole.findByAuthority('ROLE_FACEBOOK'))
    FacebookUser fbUser = new FacebookUser(
            uid: token.uid,
            accessToken: token.accessToken.accessToken,
            accessTokenExpires: token.accessToken.expireAt,
            user: person
    )
    fbUser.save(failOnError: true)
    return fbUser
}

The response is as below: access token:

{"accessToken":{"accessToken":"CAAHt52DpmkIBAH34zFdy0PZC3a9y3JZARvfrVckTHN9xMZCOgL6QHzxNIna07ZBa5ZBXw2BZCwyflvCIIkPzn3pNk8QHFNqZCts8tAHZB1wK4AEJZBEPNHqFRWssPZBuIOCa6vk7U5W3K0ilIZB7GWx0MJCZC7UBy5mhb5W5RLkZB1wTn09Qs4NIGU9KDi22OPWbpEKQJoERr9fSfJwZDZD","expireAt":"2016-02-01T06:58:46Z"},"authenticated":false,"authorities":[],"code":"AQD3IrrusGkBsF91yaD68T81S4s-TO4qG17DARQepzB-y2q4vmWSFkoTmMX_ObJHjbOg83YcSE0DwsNjvKS7U2JH_O2C6XjVGM5vG8ZvxpXTo57RFhAzvTxd0NMIVL4Qypto2VRDcLeNIIW8dCQHNqhzJ-0l3_4BQfHk4ygXFEBFTmZA6wkFspnLr9awL5qL8t0m6h5AZbkydM6KhQCYmL_xmy8Evl22NSjYRG-G-edjVNo4t4Fn9AQt7AXEjG_xjytdUoBPa0Zpl5AGM1-VpeYOvhKDPGvXQ3fFkS-8oSe8dyj1T6gAc8BG2yQ4bycPftVqfNt3uCGpesYIkI-dnwxd","credentials":1026180034068785,"details":null,"name":"","principal":null,"redirectUri":"http://localhost:8080/j_spring_security_facebook_check","uid":1026180034068785}

and fbProfile:

{"about":null,"bio":null,"birthday":null,"class":"org.springframework.social.facebook.api.FacebookProfile","education":null,"email":null,"favoriteAtheletes":null,"favoriteTeams":null,"firstName":null,"gender":null,"hometown":null,"id":"1026180034068785","inspirationalPeople":null,"interestedIn":null,"languages":null,"lastName":null,"link":null,"locale":null,"location":null,"middleName":null,"name":"Sanjib Maharjan","political":null,"quotes":null,"relationshipStatus":null,"religion":null,"significantOther":null,"sports":null,"thirdPartyId":null,"timezone":null,"updatedTime":null,"username":null,"website":null,"work":null}

The problem here is everything is null and i could not figure out why.

PS:All the facebook credential are correct and permissions are also provided(public_profile, email and user_about_me). Is is because the token is not authenticated or is there some other thing that i am missing.Any help is appreciated.Thank you.


Solution

  • I finally figured out why everything is null except name and id.

    Facebook have changed the api since July 8th, 2015 - API version 2.4 : https://developers.facebook.com/docs/apps/changelog#v2_4

    and according to that we have to specify each field and edge you want to get returned, e.g.

    https://graph.facebook.com/me?access_token=kjhgfgfhgghjhghj&fields=id,name,email,firstname

    facebook.userOperations().getUserProfile() is providing null fields except id and name.So the solution is to use different api that allows to set fields or we can do it manually by hitting the uri(https://graph.facebook.com/me?access_token=kjhgfgfhgghjhghj&fields=id,name,email,firstname).