skip to Main Content

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


  1. 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 from TwitterTemplate and make rest call to “https://api.twitter.com/1.1/account/verify_credentials.json?include_email=true“.

    Some thing like this:

    private TwitterTemplate twitterTemplate;
    
    [....]
    
    RestTemplate restTemplate = twitterTemplate.getRestTemplate();   
    String response = restTemplate.getForObject("https://api.twitter.com/1.1/account/verify_credentials.json?include_email=true", String.class);
    System.out.println("Profile Info with Email: "+ response);
    

    Check this Commit in my GitHub Spring Twitter experiments project for getting email of User.

    Login or Signup to reply.
  2. ============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 :

    return restTemplate.getForObject(buildUri("account/verify_credentials.json?include_email=true"), TwitterProfile.class);
    

    step 5.set TwitterAdapter.java’s fetchUserProfile method:

    return new UserProfileBuilder().setName(profile.getName()).setUsername(profile.getScreenName()).setEmail(profile.getEmail()).build();
    
    Login or Signup to reply.
  3. Using hint from Sanjay’s answer

           private TwitterTemplate twitterTemplate;
    
           twitterTemplate = new TwitterTemplate("Your App's Consumer Key", "Your App's Consumer Secret",connection.createData().getAccessToken(), connection.createData().getSecret());
    
           RestTemplate restTemplate = twitterTemplate.getRestTemplate();
           String response = restTemplate.getForObject("https://api.twitter.com/1.1/account/verify_credentials.json?include_email=true", String.class);
    
          JSONObject json = new JSONObject(response);
          System.out.println(json.getString("email")); 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search