title: How to fetch Facebook UserName, Email, phoneno etc from Facebook Authentication for Spring Security in Grails
body: <p>I am using <a href="http://grails.org/plugin/spring-security-core" rel="nofollow noreferrer">Spring Security Core Plugin</a> for Authentication purpose and <a href="http://grails.org/plugin/spring-security-facebook" rel="nofollow noreferrer">Facebook Authentication for Spring Security</a> for Facebook login purpose. My FB login works fine with Spring Security Plugin. But I have some issue on that. How Can I fetch UserName, Email, phoneno etc of Facebook user so that I can save it to my db.</p>
  
  <p>Here is my Spring Security Core Person.groovy Domain Class file ...</p>
  
  <pre><code>class Person {
  
  transient springSecurityService
  
  String realName
  String username
  String password
  boolean enabled
  boolean accountExpired
  boolean accountLocked
  boolean passwordExpired
  byte[] avatar
  String avatarType
      ...... more code ...........
  }
  </code></pre>
  
  <p>Here You can see I added some more fields like realName(for saving facebook user name), avatar(for saving facebook user profile image) etc. Right now Username is saving like this <code>facebook_100003641451723</code>. I want the real name of user so that i can save it to my realName field. Any idea how can i do this ??</p>
  
  <p><strong>UPDATE :</strong></p>
  
  <p>I found one solution over there <a href="https://stackoverflow.com/questions/15151717/grails-facebook-login-using-spring-security-how-to-know-logged-in-user-name">Grails Facebook Login using spring security - how to know logged in user name</a></p>
  
  <p>But I don't know where I put this code ...</p>
  
  <pre><code>void onCreate(FacebookUser user, FacebookAuthToken token) {
    Facebook facebook = new FacebookTemplate(token.accessToken.accessToken)
    FacebookProfile fbProfile = facebook.userOperations().userProfile
  
    //fill the name
    //fieldname ('fullname' at this example) is up to you
    user.realName = fbProfile.name
   }
  </code></pre>
  
  <p><strong>UPDATE :</strong></p>
  
  <p>Now I created new service by running <code>grails create-service FacebookAuthService</code> and my FacebookAuthService.groovy is created inside services/groovypublish/FacebookAuthService.groovy there..</p>
  
  <p>Here is my FacebookAuthService.groovy file ..</p>
  
  <pre><code>package groovypublish
  
  import org.grails.twitter.Person
  import com.the6hours.grails.springsecurity.facebook.FacebookAuthToken
  
  
  class FacebookAuthService {
  
     def serviceMethod() {
  
     }
  
     void onCreate(Person user, FacebookAuthToken token) {
     Facebook facebook = new FacebookTemplate(token.accessToken.accessToken)
     FacebookProfile fbProfile = facebook.userOperations().userProfile
  
     //fill the name
     //fieldname ('fullname' at this example) is up to you
     user.realName = fbProfile.name
    }
  
    }
  </code></pre>
  
  <p>and I put dependencies in my BuildConfig.groovy file ....</p>
  
  <pre><code>dependencies {
      // specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes e.g.
  
       runtime 'mysql:mysql-connector-java:5.1.22'
       compile 'org.springframework.social:spring-social-core:1.0.1.RELEASE'
       compile 'org.springframework.social:spring-social-facebook:1.0.1.RELEASE'
  }
  </code></pre>
  
  <p>Now When i start my grails application <code>grails run-app</code> it showing error</p>
  
  <pre><code>     C:\Users\Shreshtt\workspace\groovypublish\grails-app\services\groovypublish\FacebookAuthService.groovy: 14: unable to resolve class Facebook @ line 14, column 12.
   Facebook facebook = new FacebookTemplate(token.accessToken.accessToken)
            ^
  
       C:\Users\Shreshtt\workspace\groovypublish\grails-app\services\groovypublish\FacebookAuthService.groovy: 14: unable to resolve class FacebookTemplate @ line 14, column 23.
   Facebook facebook = new FacebookTemplate(token.accessToken.accessToken)
                       ^
  
       C:\Users\Shreshtt\workspace\groovypublish\grails-app\services\groovypublish\FacebookAuthService.groovy: 15: unable to resolve class FacebookProfile @ line 15, column 19.
   FacebookProfile fbProfile = facebook.userOperations().userProfile
                   ^
  
   3 errors
  </code></pre>