【问题标题】:How to get email id from facebook using Grails social plugin如何使用 Grails 社交插件从 facebook 获取电子邮件 ID
【发布时间】:2014-12-26 11:44:36
【问题描述】:

我在我的应用程序中使用 spring-security-oauth-facebook:0.1 grails 插件。现在我想访问人员的电子邮件 ID,但每次 facebook 服务器响应时我都无法响应 1829812989120 对于 socialId 、Username、profileId 并且在返回 Map 对象时没有电子邮件选项。为什么会这样。

     oauth {
        debug = true
           providers {
    facebook {
        api = org.scribe.builder.api.FacebookApi
        key = 'my-app-key'
        secret = 'secret-key'
        successUri = '/oauth/facebook/success'
        failureUri = '/oauth/facebook/failure'
        callback = "${baseURL}/oauth/facebook/callback"
        scopes = "['public_profile','email','name','user']"

      } 

    def sessionKey = oauthService.findSessionKeyForAccessToken(provider)

    if (!session[sessionKey]) {
        log.warn "No OAuth token in the session for provider '${provider}'"
        throw new OAuthLoginException("Authentication error for provider '${provider}'")
    }
    // Create the relevant authentication token and attempt to log in.

    OAuthToken oAuthToken = springSecurityOAuthService.createAuthToken(provider, 
                                                                            session[sessionKey])
    println "oAuthToken.principal = "+oAuthToken.principal.toString();
    println "oAuthToken.socialId  = "+oAuthToken.socialId;
    println "oAuthToken.properties= "+oAuthToken.properties
    println "oAuthToken.properties= "+oAuthToken.name
    println "oAuthToken.properties= "+oAuthToken.toString();

1)我检查了所有 println 值的电子邮件都不存在。 请帮助我如何获取登录用户的电子邮件地址。

【问题讨论】:

标签: grails oauth facebook-oauth


【解决方案1】:

创建会话后使用此代码

OAuthToken oAuthToken = springSecurityOAuthService.createAuthToken(provider, 
                                                                        session[sessionKey])
Token facebookAccessToken = (Token) session[oauthService.findSessionKeyForAccessToken('facebook')]

println "Facebook token :"+facebookAccessToken

def facebookResource = oauthService.getFacebookResource(facebookAccessToken ,    "https://graph.facebook.com/me")

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

【讨论】:

    【解决方案2】:

    电子邮件 不是 Facebook public_profile 的一部分。获取用户电子邮件地址的唯一方法是在电子邮件字段中请求扩展permissions。您可以通过向 oauth 提供程序添加范围来做到这一点。

    config.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 (Default)

    一个人的公开资料默认引用用户对象的以下属性:

    • 身份证封面
    • 姓名
    • 名字
    • 姓氏
    • 年龄范围
    • 链接
    • 性别
    • 语言环境
    • 图片
    • 时区
    • 更新时间
    • 已验证

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-21
      • 2015-04-03
      • 2016-09-26
      • 2018-11-03
      • 2015-03-15
      • 1970-01-01
      • 2018-10-24
      • 1970-01-01
      相关资源
      最近更新 更多