org.springframework.social.twitter
Class TwitterTemplate

java.lang.Object
  extended by org.springframework.social.twitter.TwitterTemplate
All Implemented Interfaces:
TwitterOperations

public class TwitterTemplate
extends java.lang.Object
implements TwitterOperations

This is the central class for interacting with Twitter.

Most (not all) Twitter operations require OAuth authentication. To perform such operations, TwitterTemplate must be constructed with the minimal amount of information required to sign requests to Twitter's API with an OAuth Authorization header.

There are a few operations, such as searching, that do not require OAuth authentication. In those cases, you may use a TwitterTemplate that is created through the default constructor and without any OAuth details. Attempts to perform secured operations through such an instance, however, will result in AccountNotConnectedException being thrown.


Constructor Summary
TwitterTemplate()
          Create a new instance of TwitterTemplate.
TwitterTemplate(java.lang.String apiKey, java.lang.String apiSecret, java.lang.String accessToken, java.lang.String accessTokenSecret)
          Create a new instance of TwitterTemplate.
 
Method Summary
 java.util.List<java.lang.String> getFriends(java.lang.String screenName)
          Retrieves a list of users that the given user follows.
 java.lang.String getProfileId()
          Retrieves the user's Twitter screen name.
 void retweet(long tweetId)
          Posts a retweet of an existing tweet.
 SearchResults search(java.lang.String query)
          Searches Twitter, returning the first 50 matching Tweets
 SearchResults search(java.lang.String query, int page, int resultsPerPage)
          Searches Twitter, returning a specific page out of the complete set of results.
 SearchResults search(java.lang.String query, int page, int resultsPerPage, int sinceId, int maxId)
          Searches Twitter, returning a specific page out of the complete set of results.
 void updateStatus(java.lang.String message)
          Updates the user's status.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TwitterTemplate

public TwitterTemplate()
Create a new instance of TwitterTemplate. This constructor creates a new TwitterTemplate able to perform unauthenticated operations against Twitter's API. Some operations, such as search, do not require OAuth authentication. A TwitterTemplate created with this constructor will support those operations. Those operations requiring authentication will throw AccountNotConnectedException.


TwitterTemplate

public TwitterTemplate(java.lang.String apiKey,
                       java.lang.String apiSecret,
                       java.lang.String accessToken,
                       java.lang.String accessTokenSecret)
Create a new instance of TwitterTemplate. This constructor creates a new TwitterTemplate given the minimal amount of information required to sign a request and builds up a RestOperations internally using this information.

Parameters:
apiKey - the application's API key.
apiSecret - the application's API secret.
accessToken - the user's access token, given after successful OAuth authentication.
accessTokenSecret - the access token secret, given along with the access token after successful OAuth authentication.
Method Detail

getProfileId

public java.lang.String getProfileId()
Description copied from interface: TwitterOperations
Retrieves the user's Twitter screen name.

Specified by:
getProfileId in interface TwitterOperations
Returns:
the user's screen name at Twitter

getFriends

public java.util.List<java.lang.String> getFriends(java.lang.String screenName)
Description copied from interface: TwitterOperations
Retrieves a list of users that the given user follows.

Specified by:
getFriends in interface TwitterOperations
Parameters:
screenName - The user's Twitter screen name
Returns:
a list of user screen names

updateStatus

public void updateStatus(java.lang.String message)
Description copied from interface: TwitterOperations
Updates the user's status.

Specified by:
updateStatus in interface TwitterOperations
Parameters:
message - The status message

retweet

public void retweet(long tweetId)
Description copied from interface: TwitterOperations
Posts a retweet of an existing tweet.

Specified by:
retweet in interface TwitterOperations
Parameters:
tweetId - The ID of the tweet to be retweeted

search

public SearchResults search(java.lang.String query)
Description copied from interface: TwitterOperations
Searches Twitter, returning the first 50 matching Tweets

Specified by:
search in interface TwitterOperations
Parameters:
query - The search query string
Returns:
a SearchResults containing the search results metadata and a list of matching Tweets
See Also:
SearchResults, {@link Tweet}

search

public SearchResults search(java.lang.String query,
                            int page,
                            int resultsPerPage)
Description copied from interface: TwitterOperations
Searches Twitter, returning a specific page out of the complete set of results.

Specified by:
search in interface TwitterOperations
Parameters:
query - The search query string
page - The page to return
resultsPerPage - The number of Tweets per page
Returns:
a SearchResults containing the search results metadata and a list of matching Tweets
See Also:
SearchResults, {@link Tweet}

search

public SearchResults search(java.lang.String query,
                            int page,
                            int resultsPerPage,
                            int sinceId,
                            int maxId)
Description copied from interface: TwitterOperations
Searches Twitter, returning a specific page out of the complete set of results. Results are filtered to those whose ID falls between sinceId and maxId

Specified by:
search in interface TwitterOperations
Parameters:
query - The search query string
page - The page to return
sinceId - The minimum Tweet ID to return in the results
maxId - The maximum Tweet ID to return in the results
Returns:
a SearchResults containing the search results metadata and a list of matching Tweets
See Also:
SearchResults, {@link Tweet}