为啥 Spring Social 插件偶尔会在 User 类上返回空邮件?

Posted

技术标签:

【中文标题】为啥 Spring Social 插件偶尔会在 User 类上返回空邮件?【英文标题】:Why does the Spring Social plugin occasionally return an empty email on the User class?为什么 Spring Social 插件偶尔会在 User 类上返回空邮件? 【发布时间】:2015-08-17 01:45:38 【问题描述】:

我有一个 Grails 项目 (v2.4.2),它使用 spring-security-facebook:0.17 插件通过 Spring Security 进行身份验证。乍一看,一切似乎都很好。但是,由于某些未知原因,我无法访问大量用户的电子邮件地址。我正在使用 spring social 来获取电子邮件。我有权限,它设置在范围内。这是我验证新用户的代码 sn-p:

log.info("Create domain for facebook user $token.uid")

//Use Spring Social Facebook to load details for current user from Facebook API
log.info("create: FacebookAuthToken: $token")
log.info("created FacebookAuthToken.FacebookAccessToken = $token.accessToken")

Facebook facebook = new FacebookTemplate(token.accessToken.accessToken)

org.springframework.social.facebook.api.User fbProfile = facebook.userOperations().getUserProfile()

// Check if email is actual granted because in production some are coming back null
boolean isEmailGranted=false
List<Permission> permissions = facebook?.userOperations()?.getUserPermissions()
String permissionString = "["
for (int i=0;i<permissions.size();i++) 
    permissionString += "["+ permissions[i].getName() + ":" + permissions[i].getStatus()+"]"
    if (permissions[i].getName()=="email" && permissions[i].isGranted())
        isEmailGranted=true

permissionString += "]"
log.info("create: Facebook Permissions = " + permissionString)

def grailsWebRequest = WebUtils.retrieveGrailsWebRequest()
def flash = grailsWebRequest.flashScope

if (!isEmailGranted) 
    log.warn("create: Unable to subscribe facebook user because email priviledge was not granted.")
    flash.message = 'Login to Facebook failed. We must have access to your email address in order to proceed with login.'
    throw new InsufficientAuthenticationException("Facebook email not accessible")


log.info("created: ")

String email = fbProfile.getEmail()
String firstName = fbProfile.getFirstName()
String lastName = fbProfile.getLastName()
String fullName = fbProfile.getName()
String username = firstName
String password = token.accessToken.accessToken

if (!email) 
    log.error("create: Permission was granted to use facebook email but the value is null.")
    flash.message = 'Login to Facebook failed. We are temporarily unable to access your email although permission has been granted'
    throw new InsufficientAuthenticationException("Facebook email not accessible for unknown reason")

为什么在获得许可后我会收到一封空电子邮件?是否有处理这种行为的首选方法(除了身份验证失败和伪造电子邮件地址)。非常感谢!

【问题讨论】:

【参考方案1】:

“用户”对象 (https://developers.facebook.com/docs/reference/api/user/) 的“电子邮件”字段的文档阐明了此处的预期行为,即: "如果没有可用的有效电子邮件地址,则不会返回此字段"

有一个关于不发送电子邮件的不同情况的详细说明。请检查一下: https://developers.facebook.com/bugs/298946933534016/

希望对你有帮助。

【讨论】:

谢谢 - 显然 FB 不必给出具体的答案。我有一个儿子,他的 AOL 帐户是我因为这个问题而无法进行身份验证的用户之一。我不确定最好/最实用的处理方式应该是什么。如果可以避免,我真的不想拒绝用户。我想我会继续使用假电子邮件对用户进行身份验证/创建用户,并假设他们必须在以后通过定期检查他们的 FB 电子邮件是否发生变化来进行内部身份验证。

以上是关于为啥 Spring Social 插件偶尔会在 User 类上返回空邮件?的主要内容,如果未能解决你的问题,请参考以下文章

为啥偶尔会在 ListView 中加载错误的图像?

为啥构建后步骤 (xcopy) 偶尔会在 TeamCity 构建中以代码 2 退出?

故障安全插件不会在一个项目上运行,但会在另一个项目上运行——为啥?

为啥 spring-boot 和 postgres 连接会在一段时间后断开?

为啥“category:social”在 GMail UI 搜索中使用,但在 GmailApp.search("category:social", 0, 500) 中不起作用

Grails spring-security-facebook 插件的 accessToken