简体   繁体   English

如何从Grails中的Spring Security for Facebook身份验证中获取Facebook UserName,Email,phoneno等

[英]How to fetch Facebook UserName, Email, phoneno etc from Facebook Authentication for Spring Security in Grails

I am using Spring Security Core Plugin for Authentication purpose and Facebook Authentication for Spring Security for Facebook login purpose. 我正在使用Spring Security Core Plugin进行身份验证,并使用Facebook身份验证进行Spring登录。 My FB login works fine with Spring Security Plugin. 我的FB登录适用于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. 如何获取Facebook用户的UserName,Email,phoneno等,以便我可以将其保存到我的数据库中。

Here is my Spring Security Core Person.groovy Domain Class file ... 这是我的Spring Security Core Person.groovy Domain Class文件......

class Person {

transient springSecurityService

String realName
String username
String password
boolean enabled
boolean accountExpired
boolean accountLocked
boolean passwordExpired
byte[] avatar
String avatarType
    ...... more code ...........
}

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 facebook_100003641451723 . 在这里您可以看到我添加了一些更多的字段,如realName(用于保存Facebook用户名),头像(用于保存Facebook用户个人资料图像)等。现在用户名正在保存这样的facebook_100003641451723 I want the real name of user so that i can save it to my realName field. 我想要用户的真实姓名,以便我可以将其保存到我的realName字段。 Any idea how can i do this ?? 知道怎么办?

UPDATE : 更新:

I found one solution over there Grails Facebook Login using spring security - how to know logged in user name 我在那里找到了一个解决方案Grails Facebook使用spring security登录 - 如何知道登录的用户名

But I don't know where I put this 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
 }

UPDATE : 更新:

Now I created new service by running grails create-service FacebookAuthService and my FacebookAuthService.groovy is created inside services/groovypublish/FacebookAuthService.groovy there.. 现在我通过运行grails create-service FacebookAuthService新服务grails create-service FacebookAuthService ,我的FacebookAuthService.groovy在services / groovypublish / FacebookAuthService.groovy里面创建。

Here is my FacebookAuthService.groovy file .. 这是我的FacebookAuthService.groovy文件..

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
  }

  }

and I put dependencies in my BuildConfig.groovy file .... 我把依赖项放在BuildConfig.groovy文件中....

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'
}

Now When i start my grails application grails run-app it showing error 现在当我开始我的grails应用程序grails run-app它显示错误

     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

You have to put it into your implementation of FacebookAuthService . 你必须把它放到你的FacebookAuthService实现中。

Just create service with such name, and add method onCreate with this code (or similar). 只需使用此类名称创建服务,并使用此代码(或类似代码)添加onCreate方法。 And don't forget ot add Spring Social Facebook as a dependency 并且不要忘记将Spring Social Facebook添加为依赖项

我建议您应该使用Oauth插件 ,请在此处查看: http//grails.org/plugin/oauth我正在使用此插件开发Grails项目,我的项目包括Facebook身份验证,现在它的工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM