为什么Spring Social插件偶尔会在User类上返回一个空的电子邮件?

时间:2015-06-03 03:00:44

标签: facebook grails spring-security spring-social

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

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 个答案:

答案 0 :(得分:0)

&#39;电子邮件&#39;的文档&#39;用户&#39;的字段object(https://developers.facebook.com/docs/reference/api/user/)澄清了这里的预期行为,即: &#34;如果没有有效的电子邮件地址,则不会返回此字段&#34;

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

我希望它有所帮助。