用户';s的电子邮件始终为空-Spring社交Facebook登录
据介绍,只要用户在使用Facebook登录时接受并授予权限,任何应用程序都可以访问电子邮件 我已经仔细检查了我们的应用程序定义,我可以看到电子邮件应该可以访问 我正在使用Spring Social提供的最新版本用户';s的电子邮件始终为空-Spring社交Facebook登录,spring,facebook,email,facebook-graph-api,Spring,Facebook,Email,Facebook Graph Api,据介绍,只要用户在使用Facebook登录时接受并授予权限,任何应用程序都可以访问电子邮件 我已经仔细检查了我们的应用程序定义,我可以看到电子邮件应该可以访问 我正在使用Spring Social提供的最新版本 <spring-social-security>1.1.4.RELEASE</spring-social-security> <spring-social-core>1.1.4.RELEASE</spring-social-core> &
<spring-social-security>1.1.4.RELEASE</spring-social-security>
<spring-social-core>1.1.4.RELEASE</spring-social-core>
<spring-social-facebook>2.0.3.RELEASE</spring-social-facebook>
1.1.4.1版本
1.1.4.1发布
2.0.3.1发布
用户使用Facebook以及Google+和Twitter成功登录
不幸的是,除了我自己的用户之外,所有情况下的电子邮件都是空的
但有两种情况下电子邮件可能为空
- 如果有人使用电话号码而不是电子邮件地址注册Facebook,则电子邮件字段可能为空
- 如果没有有效的电子邮件地址,则不会返回此字段
有什么建议吗?谢谢。问题可能是您试图获取用户名。目前,Facebook取消了用户名,因为用户名是通过Facebook发送电子邮件的一种方式 您可以尝试删除此行:
final String uemail = userProfile.getUsername();
并检查是否能收到电子邮件。
这是我用来获取电子邮件等的代码:
OAuth2Operations oauthOperations = fbProvider.getOAuthOperations();
AccessGrant accessGrant = oauthOperations.exchangeForAccess(authorizationCode, facebookCallback, null);
String accessToken = accessGrant.getAccessToken();
Connection<Facebook> connection = fbConnectionFactory.createConnection(accessGrant);
UserProfile profile = connection.fetchUserProfile();
ConnectionKey connectionKey = connection.getKey();
String usn = connectionKey.getProviderUserId();
String email = profile.getEmail();
OAuth2Operations OAuthoOperations=fbProvider.getOAuthoOperations();
AccessGrant=OAuthoOperations.exchangeForAccess(授权代码,facebookCallback,null);
字符串accessToken=accessGrant.getAccessToken();
Connection Connection=fbConnectionFactory.createConnection(accessGrant);
UserProfile=connection.fetchUserProfile();
ConnectionKey ConnectionKey=connection.getKey();
字符串usn=connectionKey.getProviderUserId();
字符串email=profile.getEmail();
我找到了问题所在。我没有向社交连接工厂添加所需的作用域
FacebookConnectionFactory fcf = new FacebookConnectionFactory(
env.getProperty("oauth.facebook.api.key"),
env.getProperty("oauth.facebook.api.secret"));
//this is the important bit
fcf.setScope("public_profile,email");
.....
电子邮件丢失。正如@felipe提到的,在spring social的facebook登录期间,需要设置获取电子邮件的权限/范围 如果您使用XML格式而不是java类进行配置,那么可以按如下方式定义范围
<bean id="connectionFactoryRegistry"
class="org.springframework.social.security.SocialAuthenticationServiceRegistry">
<property name="authenticationServices">
<util:list>
<bean class="org.springframework.social.facebook.security.FacebookAuthenticationService">
<constructor-arg value="${facebook.clientId}"/>
<constructor-arg value="${facebook.clientSecret}"/>
<property name="defaultScope" value="email"/>
</bean>
</util:list>
</property>
</bean>
<alias name="connectionFactoryRegistry" alias="connectionFactoryLocator"/>
你能发布你的代码吗?@m.aibin公共字符串执行(final org.springframework.social.connect.Connection连接){//将新用户添加到数据库中,并将其默认角色用于SocialAuthenticationSuccessHandler final org.springframework.social.connect.UserProfile UserProfile=Connection.fetchUserProfile();最终字符串uemail=userProfile.getUsername();最终字符串uname=userProfile.getEmail()
感谢您指出我将username分配给email变量,将email分配给username变量的事实。完全相反!话虽如此,我仍然看不出您的代码和我的代码有什么不同。最终您从UserProfile获得电子邮件,这与我通过执行UserProfile profile=connecti进行实例化的方式相同on.fetchUserProfile()上
无论如何,我现在正在测试它…运气不好。电子邮件总是空的。奇怪的是,我无法从GraphAPI浏览器检索我的朋友的电子邮件,即使我的朋友有一个经过验证的帐户,并且他在隐私中设置了朋友可以看到电子邮件。这意味着我应该访问它,但我不能从GraphAPI浏览器。我可以从UI中看到它但没有别的办法。你是否检查过你的代码,有效地为你自己以外的其他用户检索电子邮件?