0

它是一个 grails 项目,
Facebook 身份验证通过 oauth 成功,
现在当它返回到我的控制器时,我想获取登录用户的 emailID,
搜索了很多,但没有找到合适的文档,
我正在使用 scribe 并且有以下内容Config.groory 中的代码

import org.scribe.builder.api.FacebookApi

oauth {
providers {
    facebook {
        api = FacebookApi
        key = 'xxxx'
        secret = 'yyyy'
        callback = "http://my-domain-name-here:8080/TestOAuth2/dashBoard/facebooklogin"
        successUri = "http://my-domain-name-here:8080/TestOAuth2/dashBoard/success"
    }
}
}

非常感谢任何帮助。
谢谢。

4

2 回答 2

3

尝试这个..,。

配置:

import org.scribe.builder.api.FacebookApi
...
oauth {
  providers {
    facebook {
      api = FacebookApi

      key = 'XXX'
      secret = 'YYY'

      scope = 'email,read_stream,publish_actions,user_birthday,publish_stream'

      callback = "http://localhost:8080/appName/oauth/facebook/callback"   //callback to oauth controller of oauth plugin

      successUri = "http://localhost:8080/appName/myController/facebookSuccess"
      failureUri = "http://localhost:8080/appName/myController/facebookFailure"
    }
  }
}

我的控制器:

def facebookSuccess() {
    Token facebookAccessToken = (Token) session[oauthService.findSessionKeyForAccessToken('facebook')]
    def facebookResource = oauthService.getFacebookResource(facebookAccessToken, "https://graph.facebook.com/me")
    def facebookResponse = JSON.parse(facebookResource?.getBody())

    log.info "Email = ${facebookResponse.email}"
    ...
}

你可以从我的 git repo 获得工作示例。Grails Oauth 插件演示

于 2013-11-08T19:08:03.100 回答
0

电子邮件不是Facebook public_profile的一部分。获取用户电子邮件地址的唯一方法是请求电子邮件字段的扩展权限。您可以通过向oauth 提供程序添加范围来执行此操作。

配置.groovy

oauth {
   providers {
      facebook { 
          api = org.scribe.builder.api.FacebookApi
          scope = 'email'
          ...
          ...
      } 
   }
}

作为如何返回的示例,emailvarious public_profile fields参见下文。 注意: getFacebookResource参数例如https://graph.facebook.com/me?fields=id,name,verified,age_range,email"

import grails.converters.JSON
import org.scribe.model.Token
import grails.plugin.springsecurity.oauth.OAuthToken

class SpringSecurityOAuthController {

   def oauthService

   def onSuccess = {
      // Validate the 'provider' URL. Any errors here are either misconfiguration
      // or web crawlers (or malicious users).
      if (!params.provider) {
        renderError 400, "The Spring Security OAuth callback URL must include the 'provider' URL parameter."
        return
      }

      def sessionKey = oauthService.findSessionKeyForAccessToken(params.provider)
      if (!session[sessionKey]) {
          renderError 500, "No OAuth token in the session for provider '${params.provider}'!"
          return
      }

      // Create the relevant authentication token and attempt to log in.
      OAuthToken oAuthToken = createAuthToken(params.provider, session[sessionKey])

      Token facebookAccessToken = (Token) session[oauthService.findSessionKeyForAccessToken('facebook')]

      def facebookResource = oauthService.getFacebookResource(facebookAccessToken ,    "https://graph.facebook.com/me?fields=id,name,verified,age_range,email")

      def facebookResponse = JSON.parse(facebookResource?.getBody())

      println facebookResponse

      ...
      ...
    }
}

public_profile(默认)

默认情况下,一个人的公共配置文件引用用户对象的以下属性:

  • 身份证封面
  • 姓名
  • 年龄范围
  • 关联
  • 性别
  • 语言环境
  • 图片
  • 时区
  • 更新时间
  • 已验证
于 2017-01-28T03:43:48.567 回答