Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

user's email is always NULL - Spring Social Facebook Login

According to Facebook's official documentation, email access is given to any app provided user accepts and grants permission when login using Facebook.

I've double checked our app definition and I can see email should be accessible.

enter image description here

I'm using the latest available from Spring Social

<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>

Users successfully login using Facebook as a provider as well as Google+ and Twitter.

Unfortunately for all cases email is NULL except for my own user.

There are two scenarios where email could be null though

  • if someone signed up for Facebook with a phone number instead of an email address, the email field may be empty
  • This field will not be returned if no valid email address is available

I've triple checked that by accessing a friend's of mine account which

  1. It's got a valid email verified
  2. It's got a phone number associated with it
  3. We are friends with each other
  4. His phone and email are visible for his friends

I go to the GraphApi explorer but no email is ever returned.

Any advice? Thank you.

like image 897
felipe Avatar asked Feb 08 '23 11:02

felipe


2 Answers

I found where the problem was. I was not adding the required scopes to the social connection factory.

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");

   .....

email was missing.

like image 54
felipe Avatar answered Feb 10 '23 01:02

felipe


Maybe the problem is that you tries to fetch username. Currently, Facebook got rid of the username because the username is one way of sending emails via Facebook.

You can try to delete this line:

final String uemail = userProfile.getUsername();

and check if you can get email. Here is my code that I use to fetch email etc.:

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();
like image 22
m.aibin Avatar answered Feb 10 '23 01:02

m.aibin