我有一个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")
}
授予权限后,为什么会收到一封空电子邮件?是否有处理此行为的首选方法(除了无法通过身份验证和伪造电子邮件地址外)。非常感谢!
最佳答案
“用户”对象(https://developers.facebook.com/docs/reference/api/user/)的“电子邮件”字段的文档在此阐明了预期的行为,即:
“如果没有有效的电子邮件地址,则不会返回此字段”
有关不会发送电子邮件的不同情况的详细说明。请检查一下:
https://developers.facebook.com/bugs/298946933534016/
希望对您有所帮助。
关于facebook - 为什么Spring Social插件偶尔会在User类上返回空电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30610104/