繁体   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

我正在使用Spring Security Core Plugin进行身份验证,并使用Facebook身份验证进行Spring登录。 我的FB登录适用于Spring Security Plugin。 但我有一些问题。 如何获取Facebook用户的UserName,Email,phoneno等,以便我可以将其保存到我的数据库中。

这是我的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 ...........
}

在这里您可以看到我添加了一些更多的字段,如realName(用于保存Facebook用户名),头像(用于保存Facebook用户个人资料图像)等。现在用户名正在保存这样的facebook_100003641451723 我想要用户的真实姓名,以便我可以将其保存到我的realName字段。 知道怎么办?

更新:

我在那里找到了一个解决方案Grails Facebook使用spring security登录 - 如何知道登录的用户名

但我不知道我把这段代码放在哪里......

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
 }

更新:

现在我通过运行grails create-service FacebookAuthService新服务grails create-service FacebookAuthService ,我的FacebookAuthService.groovy在services / groovypublish / FacebookAuthService.groovy里面创建。

这是我的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
  }

  }

我把依赖项放在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'
}

现在当我开始我的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

你必须把它放到你的FacebookAuthService实现中。

只需使用此类名称创建服务,并使用此代码(或类似代码)添加onCreate方法。 并且不要忘记将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