This post originated from an RSS feed registered with Java Buzz
by Cal Holman.
Original Post: Flickr API using OAuth - Jersey and Struts
Feed Title: Cal Holman's Blog
Feed URL: http://www.calandva.com/holmansite/do/rss/CreateRssFile?type=blog
Feed Description: CalAndVA.com is built on many Java Open Source projects - this is a blog on the site progress
This is an example of the three steps used to establish the authentication for OAuth with the Flickr site. And an example of using the API to access a photo's information.
First an Action class to use for the OAuth communication and call back - and then a DAO used for OAuth communication and upload
For the struts-config.xml - use this URL for initiating the OAuth process and call back URL on the second step
// Set the OAuth parameters OAuthSecrets secrets = new OAuthSecrets().consumerSecret(CONSUMER_SECRET).tokenSecret(OAUTH_TOKEN_SECRET); OAuthParameters params = new OAuthParameters().consumerKey(CONSUMER_KEY).signatureMethod("HMAC-SHA1").version( "1.0").token(OAUTH_TOKEN);
// Create the OAuth client filter OAuthClientFilter filter = new OAuthClientFilter(client.getProviders(), params, secrets);
// Add the filter to the resource resource.addFilter(filter);
File thePhotoFile = new File("F:/Pictures/for_site/" + thePicture.getFilename());
FormDataMultiPart form = new FormDataMultiPart(); FormDataBodyPart formBody = new FileDataBodyPart("photo", thePhotoFile, MediaType.MULTIPART_FORM_DATA_TYPE); form.bodyPart(formBody); form.field("title", thePicture.getTitle()); form.field("description", thePicture.getDescription()); form.field("tags", keywords);
// Flickr wants signature to have the parameters in signature except for photo // Jersey will not add form data to signature - so we trick it // by adding the same parameters to the reuest parms - so Jersey will // calculate the correct signature - but Flickr will ignore // the parameters MultivaluedMapImpl requestParams = new MultivaluedMapImpl(); requestParams.add("title", thePicture.getTitle()); requestParams.add("description", thePicture.getDescription()); requestParams.add("tags", keywords);
int oauth_token_index_start = responseBody.indexOf("<photoid>") + 9; int oauth_token_index_end = responseBody.indexOf("</photoid>"); String photoId = responseBody.substring(oauth_token_index_start, oauth_token_index_end);
return photoId; }
public void getFlickrInfo(String photo) throws JSONException { // Create a Jersey client Client client = Client.create();
// Create a resource to be used to make API calls WebResource resource = client.resource(URL_API);
// Set the OAuth parameters OAuthSecrets secrets = new OAuthSecrets().consumerSecret(CONSUMER_SECRET).tokenSecret(OAUTH_TOKEN_SECRET); OAuthParameters params = new OAuthParameters().consumerKey(CONSUMER_KEY). signatureMethod("HMAC-SHA1").version("1.0").token(OAUTH_TOKEN);
// Create the OAuth client filter OAuthClientFilter filter = new OAuthClientFilter(client.getProviders(), params, secrets);
// Add the filter to the resource resource.addFilter(filter);
Date today = new Date(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); String todayParsed = format.format(today);
cat.debug("back from flickr:\n" + response.toString() + "\n"); cat.debug("back from flickr:\n" + responseBody + "\n"); }
public String getRequestToken() {
// Create a Jersey client Client client = Client.create();
// Create a resource to be used to make API calls WebResource resource = client.resource(URL_REQUEST_TOKEN);
// Set the OAuth parameters OAuthSecrets secrets = new OAuthSecrets().consumerSecret(CONSUMER_SECRET); OAuthParameters params = new OAuthParameters().consumerKey(CONSUMER_KEY) .signatureMethod("HMAC-SHA1").version("1.0") .callback("http://www.calandva.test/holmansite/do/OAuth");
// Create the OAuth client filter OAuthClientFilter filter = new OAuthClientFilter(client.getProviders(), params, secrets);
// Add the filter to the resource resource.addFilter(filter);
// make the request String response = resource.get(String.class);
cat.debug("back from request token:" + response );
// hacked to extract the reuest_token and request_secret from response int oauth_token_index_start = 42; int oauth_token_index_end = response.indexOf("oauth_token_secret=") - 1; String oauth_token = response.substring(oauth_token_index_start, oauth_token_index_end);
int oauth_token_secret_start = response.indexOf("oauth_token_secret=")+ 19; String oauth_token_secret = response.substring(oauth_token_secret_start);
// open the browser at the authorization URL to let user authorize try { Desktop.getDesktop().browse(new URI(URL_AUTHORIZE + "?oauth_token=" + oauth_token + "&perms=write")); } catch (IOException e) { cat.fatal("Desktop failure"); e.printStackTrace(); } catch (URISyntaxException e) { cat.fatal("Desktop failure"); e.printStackTrace(); }
return oauth_token_secret; }
public void performAuthStep(String tokenSecret, String oauth_token, String oauth_verifier) { // Create a Jersey client Client client = Client.create();
// make an API call to request the access token WebResource resource = client.resource(URL_ACCESS_TOKEN);
// Set the OAuth parameters OAuthSecrets secrets = new OAuthSecrets().consumerSecret(CONSUMER_SECRET).tokenSecret(tokenSecret); OAuthParameters params = new OAuthParameters().consumerKey(CONSUMER_KEY). signatureMethod("HMAC-SHA1").version("1.0");
// use the request token id and secret to create the request