-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get Logged-In User's Email ID from Twitter Account
RestTemplate and JSON to POJO Method - Converted JSON returned from https://api.twitter.com/1.1/account/verify_credentials.json?include_email=true to POJO.
- Loading branch information
Showing
8 changed files
with
1,211 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/main/java/com/rawsanj/tweet/controller/UserInfoController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.rawsanj.tweet.controller; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.springframework.social.connect.Connection; | ||
import org.springframework.social.connect.ConnectionRepository; | ||
import org.springframework.social.connect.UserProfile; | ||
import org.springframework.social.twitter.api.CursoredList; | ||
import org.springframework.social.twitter.api.Twitter; | ||
import org.springframework.social.twitter.api.TwitterProfile; | ||
import org.springframework.social.twitter.api.impl.TwitterTemplate; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.Model; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import com.rawsanj.tweet.util.TwitterProfileWithEmail; | ||
|
||
@Controller | ||
@RequestMapping("/userinfo") | ||
public class UserInfoController { | ||
|
||
private Twitter twitter; | ||
|
||
private ConnectionRepository connectionRepository; | ||
|
||
private TwitterTemplate twitterTemplate; | ||
|
||
@Inject | ||
public UserInfoController(Twitter twitter, ConnectionRepository connectionRepository, TwitterTemplate twitterTemplate) { | ||
this.twitter = twitter; | ||
this.connectionRepository = connectionRepository; | ||
this.twitterTemplate = twitterTemplate; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
RawSanj
Author
Owner
|
||
} | ||
|
||
@RequestMapping(method=RequestMethod.GET) | ||
public String helloTwitter(Model model) { | ||
if (connectionRepository.findPrimaryConnection(Twitter.class) == null) { | ||
return "redirect:/connect/twitter"; | ||
} | ||
|
||
Connection<Twitter> con = connectionRepository.findPrimaryConnection(Twitter.class); | ||
//ConnectionKey key = con.getKey(); | ||
UserProfile userProfile = con.fetchUserProfile(); | ||
System.out.println("Email address of Logged In User: "+ userProfile.getEmail()); | ||
System.out.println("Name of Logged In User: "+ userProfile.getFirstName() + " " + userProfile.getLastName()); | ||
|
||
RestTemplate restTemplate = twitterTemplate.getRestTemplate(); | ||
|
||
TwitterProfileWithEmail response = restTemplate.getForObject("https://api.twitter.com/1.1/account/verify_credentials.json?include_email=true", TwitterProfileWithEmail.class); | ||
System.out.println("Name: "+ response.getName()+ " Email: "+ response.getEmail()); | ||
|
||
model.addAttribute("accountInfo", response); | ||
return "hello"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
|
||
package com.rawsanj.tweet.util; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import javax.annotation.Generated; | ||
import com.fasterxml.jackson.annotation.JsonAnyGetter; | ||
import com.fasterxml.jackson.annotation.JsonAnySetter; | ||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.annotation.JsonPropertyOrder; | ||
|
||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
@Generated("org.jsonschema2pojo") | ||
@JsonPropertyOrder({ | ||
"urls" | ||
}) | ||
public class Description { | ||
|
||
@JsonProperty("urls") | ||
private List<Object> urls = new ArrayList<Object>(); | ||
@JsonIgnore | ||
private Map<String, Object> additionalProperties = new HashMap<String, Object>(); | ||
|
||
/** | ||
* | ||
* @return | ||
* The urls | ||
*/ | ||
@JsonProperty("urls") | ||
public List<Object> getUrls() { | ||
return urls; | ||
} | ||
|
||
/** | ||
* | ||
* @param urls | ||
* The urls | ||
*/ | ||
@JsonProperty("urls") | ||
public void setUrls(List<Object> urls) { | ||
this.urls = urls; | ||
} | ||
|
||
@JsonAnyGetter | ||
public Map<String, Object> getAdditionalProperties() { | ||
return this.additionalProperties; | ||
} | ||
|
||
@JsonAnySetter | ||
public void setAdditionalProperty(String name, Object value) { | ||
this.additionalProperties.put(name, value); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
|
||
package com.rawsanj.tweet.util; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import javax.annotation.Generated; | ||
import com.fasterxml.jackson.annotation.JsonAnyGetter; | ||
import com.fasterxml.jackson.annotation.JsonAnySetter; | ||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.annotation.JsonPropertyOrder; | ||
|
||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
@Generated("org.jsonschema2pojo") | ||
@JsonPropertyOrder({ | ||
"description" | ||
}) | ||
public class Entities { | ||
|
||
@JsonProperty("description") | ||
private Description description; | ||
@JsonIgnore | ||
private Map<String, Object> additionalProperties = new HashMap<String, Object>(); | ||
|
||
/** | ||
* | ||
* @return | ||
* The description | ||
*/ | ||
@JsonProperty("description") | ||
public Description getDescription() { | ||
return description; | ||
} | ||
|
||
/** | ||
* | ||
* @param description | ||
* The description | ||
*/ | ||
@JsonProperty("description") | ||
public void setDescription(Description description) { | ||
this.description = description; | ||
} | ||
|
||
@JsonAnyGetter | ||
public Map<String, Object> getAdditionalProperties() { | ||
return this.additionalProperties; | ||
} | ||
|
||
@JsonAnySetter | ||
public void setAdditionalProperty(String name, Object value) { | ||
this.additionalProperties.put(name, value); | ||
} | ||
|
||
} |
Oops, something went wrong.
How did you get a value of twitterTemplate?