I’m trying to add “Sign in with Twitter” thing to my web application. I configured spring security and social and everything works i.e. I can fetch user name, access key, secret etc., but when I want to get user email from fetched profile I’m getting null.
My code inside of controller (spring redirect to this after successful authorization) looks like this:
Connection<A> connection = providerSignInUtils.getConnectionFromSession(request);
String email = connection.fetchUserProfile().getEmail();
I know you have to whitelist application, check “Request email addresses from users” field, refresh tokens etc. I did that all already and still getting null. I’m out of ideas what to do, I tried “/auth/twitter?scope=email” instead of “/auth/twitter” but does not work. Maybe it’s something wrong with Spring?
I’m using newest spring-social 1.1.4 with spring twitter API 1.1.2.
I saw many question about this issue but all of them were from 2011/2012, AFAIK Twitter didn’t support fetching email address from user back then, but it changed, however it still doesn’t seem to work properly.
3
Answers
There is an Open issue regarding for this.
See getEmail of org.springframework.social.connect.UserProfile returns null even if additional permissions granted.
As a workaround you can use the
RestTemplate
which you can grab fromTwitterTemplate
and make rest call to “https://api.twitter.com/1.1/account/verify_credentials.json?include_email=true“.Some thing like this:
Check this Commit in my GitHub Spring Twitter experiments project for getting email of User.
============In twitter application management============
step 1.check select “Request email addresses from users”
step 2.Permission set ReadOnly.
============edit your social-login-twitter code============
step 3.TwitterProfile.java add email field.
step 4.set UserTemplate.java’s getUserProfile method :
step 5.set TwitterAdapter.java’s fetchUserProfile method:
Using hint from Sanjay’s answer