springtwitterspring-socialspring-social-twitter

Manually connect to a user's twitter account - Spring Social


I'm running a background task and in this task I want to consult the UserConnection database and manually connect to each user's twitter account and pull some data from twitter using users operation.

The table provides the following fields

 USERID
 PROVIDERID
 PROVIDERUSERID
 RANK
 DISPLAYNAME
 PROFILEURL
 IMAGEURL
 ACCESSTOKEN
 SECRET
 REFRESHTOKEN
 EXPIRETIME

How do I use these to connect to the user's account (PROVIDERID = "twitter")?


Solution

  • After pulling the connections from the database, all I had to do was create a Twitter from TwitterTemplate.

    List<Connection> connections = connectionRepository.findAllConnections();
    for (Connection conn : connections) {
        Twitter twitter = new TwitterTemplate(twitterAppKey, twitterAppSecret, conn.getAccessToken(), conn.getSecret());
        //now I can run the operations
        System.out.println(twitter.userOperations().getScreenName());
    }