Grails Facebook使用spring security登录 - 如何知道登录的用户名

Sou*_*thu 2 grails facebook spring-security

在Grails应用程序中,使用spring security facebook插件,当用户登录时,应用程序如何获得实际的facebook用户名,以便主页可以显示登录的用户名?

Igo*_*nov 11

该插件提供Facebook API access_token,您必须从Facebook请求此类用户详细信息.

例:

添加对Spring Social Facebook的依赖:

dependency {
  compile 'org.springframework.social:spring-social-core:1.0.1.RELEASE'
  compile 'org.springframework.social:spring-social-facebook:1.0.1.RELEASE'
}
Run Code Online (Sandbox Code Playgroud)

然后通过在FacebookAuthService中添加以下内容,在用户创建时从Facebook加载它:

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.fullname = fbProfile.name
}
Run Code Online (Sandbox Code Playgroud)

也可以看看