Twitter client with Spring Social


Integrating social network activity into (customer) websites and because I have been using the Spring Framework and also Spring MVC and Spring Security for a long time, I decided to take a closer look at the Spring Social project. So I created a small application that should just display the timeline of my Twitter accounts and have the possibility to post something quickly.


Yes, I know my .css skills are incredible. 😉 I was missing an example to “quick and dirty” connect (and so authorise your social app to your account) and getting back the access token for testing the API without using a database or Spring Security on Spring Social samples, so I created a very simple way to connect to a Twitter account:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@Controller
public class AuthController {
  private OAuth1Operations oauthOperations;
  private OAuthToken requestToken;
 
  @RequestMapping(value = "/connect", method = RequestMethod.GET)
  public String connect() {
    TwitterConnectionFactory connectionFactory = new TwitterConnectionFactory(CONSUMER_KEY, CONSUMER_SECRET);
    oauthOperations = connectionFactory.getOAuthOperations();
    requestToken = oauthOperations.fetchRequestToken("http://localhost:8080/callback", null);
    String authorizeUrl = oauthOperations.buildAuthorizeUrl(requestToken.getValue(), OAuth1Parameters.NONE);
    return "redirect:" + authorizeUrl;
  }
 
  @RequestMapping(value = "/callback")
  public @ResponseBody OAuthToken callback(@RequestParam(value = "oauth_token") String oauthToken, @RequestParam(value = "oauth_verifier") String oauthVerifier) {
    OAuthToken accessToken = oauthOperations.exchangeForAccessToken(new AuthorizedRequestToken(requestToken, oauthVerifier), OAuth1Parameters.NONE);
    return accessToken;
  }
}

, , , , ,