// retrieve consumer key/secret and token/secret from the property file
// or system properties
loadSettings();
// create a new Jersey client
Client client = Client.create();
// create a new auth handler
OAuthClientFilter.AuthHandler authHandler = new OAuthClientFilter.AuthHandler() {
@Override
public void authorized(String token, String tokenSecret) {
// we received an authorized access token - store it for future runs
PROPERTIES.setProperty(PROPERTY_TOKEN, token);
PROPERTIES.setProperty(PROPERTY_TOKEN_SECRET, tokenSecret);
}
@Override
public String authorize(URI authorizationUri) {
try {
// we don't have an authorized access token
// ask user to provide authorization and return the generated
// verifier code
System.out.println("Enter the following URI into a web browser and authorize me:");
System.out.println(authorizationUri);
System.out.print("Enter the authorization code: ");
return IN.readLine();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
};
// create a new OAuth client filter passing the needed info as well as the AuthHandler
OAuthClientFilter filter = new OAuthClientFilter(
client.getProviders(),
new OAuthParameters().consumerKey(PROPERTIES.getProperty(PROPERTY_CONSUMER_KEY))
.token(PROPERTIES.getProperty(PROPERTY_TOKEN)),
new OAuthSecrets().consumerSecret(PROPERTIES.getProperty(PROPERTY_CONSUMER_SECRET))
.tokenSecret(PROPERTIES.getProperty(PROPERTY_TOKEN_SECRET)),
"http://twitter.com/oauth/request_token",
"http://twitter.com/oauth/access_token",
"http://twitter.com/oauth/authorize",
authHandler);
// Add filter to the client
client.addFilter(filter);
// make requests to protected resources
// (no need to care about the authorization flow)
do {
List<Status> statuses = client.resource(FRIENDS_TIMELINE_URI)
.get(new GenericType<List<Status>>() {});
for (Status s : statuses) {
System.out.println(s.getText());
System.out.println("[posted by " + s.getUser().getName() + " at " + s.getCreatedAt() + "]");
}