Reputation: 168
I am using Spring social Facebook framework 2.0.2 and I am accessing an Facebook app version 2.5.
I, somehow, cannot grab the email address of the user that is authenticating via the fetchObject method.
@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";
}
Id, first name and last name works perfectly but the email address seems to be null. I have registered an email address to Facebook, verified it correctly and is publicly available though.
My first guess is that the version used by Spring social Facebook is 2.3 when I look into the code. Do you think that might be that?
Upvotes: 0
Views: 503
Reputation: 168
I am replying to my question because I found what I was missing.
You actually need to give the permissions within the signin form in an hidden input. Pretty similar to the way you do it for the CSRF token.
<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>
Permissions values can be found here on Facebook dev website.
Thanks to CBroe for his answer who made me look at this in the spring documentation. I wasn't aware of it.
Upvotes: 0
Reputation:
changes in facebook api might have caused this. Try the code below:
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);
Upvotes: 1