2015-11-06 90 views
0

我使用的是Spring社交Facebook框架2.0.2,我正在访问Facebook应用程序版本2.5。春季社交Facebook 2.0.2无法获取电子邮件

我以某种方式无法获取通过fetchObject方法进行身份验证的用户的电子邮件地址。

@RequestMapping("/register/facebook") 
public String registerFacebookUser(WebRequest request, Model model) { 
    Connection<Facebook> connection = (Connection<Facebook>)providerSignInUtils.getConnectionFromSession(request); 

    User profile = connection.getApi().fetchObject("me", User.class, "id", "first_name", "last_name", "link", "email"); 

    userConnectionRepository.createConnectionRepository(profile.getId()).addConnection(connection); 

    return "register"; 
} 

身份证,名字和姓氏完美,但电子邮件地址似乎为空。我已经向Facebook注册了一个电子邮件地址,并对其进行了正确验证,并且已公开发布。

我的第一个猜测是,当我查看代码时,Spring社交Facebook使用的版本是2.3。你认为那可能是?

+1

您是否要求用户提供相应的权限? – CBroe

回答

0

我回答了我的问题,因为我发现我错过了。

您实际上需要在隐藏输入中为登录表单提供权限。非常类似于您为CSRF令牌执行的操作。

 <form id="fbk_signin" action="<c:url value="/signin/facebook"/>" method="POST">     
      <button type="submit"> 
       SignIn with facebook 
      </button> 
      <input type="hidden" 
        name="${_csrf.parameterName}" 
        value="${_csrf.token}"/> 
      <input type="hidden" name="scope" value="public_profile,email" /> 
     </form> 

在Facebook开发者网站上可以找到权限值here

感谢CBroe的回答,他让我在春季文档中看到了这一点。我没有意识到这一点。

1

facebook api中的变化可能导致了这种情况。请尝试下面的代码:

UserOperations userOperations = facebook.userOperations(); 
      String [] fields = { "id", "email", "first_name", "last_name" }; 
      org.springframework.social.facebook.api.User profile = facebook.fetchObject 
        ("me", org.springframework.social.facebook.api.User.class, fields); 
相关问题