This post originated from an RSS feed registered with Java Buzz
by Cal Holman.
Original Post: Smugmug API using 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
public void getSecurity(HttpServletRequest request) {
// 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").timestamp().nonce();
// 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 tokenResponse = resource.get(String.class);
cat.debug("Response - " + tokenResponse);
int oauth_token_index_start = 12; int oauth_token_index_end = tokenResponse.indexOf("oauth_token_secret=") - 1; String oauth_token = tokenResponse.substring(oauth_token_index_start, oauth_token_index_end);
int oauth_token_secret_start = tokenResponse.indexOf("oauth_token_secret=") + 19; int oauth_token_secret_end = oauth_token_secret_start + 64; String oauth_token_secret = tokenResponse.substring(oauth_token_secret_start, oauth_token_secret_end);
// open the browser at the authorization URL to let user authorize // Call Back URL loaded into Control Panel API Keys in "oauth callback url" field // Once user has accepted the terms the user will be forwarded to the "oauth callback url" try { Desktop.getDesktop().browse(new URI(URL_AUTHORIZE + "?oauth_token=" + oauth_token)); } catch (IOException e) { e.printStackTrace(); } catch (URISyntaxException e) { e.printStackTrace(); }
return; }
/** Category definition for this class to log to log4j. */ protected Logger cat = Logger.getLogger(this.getClass());
String CONSUMER_KEY = "<your API key>"; String CONSUMER_SECRET = "<your API secret>";
/** * This Action is to respond to a call back request from user authentication during the OAuth * process. * * @author Cal Holman */ public class OAuthCallBackAction extends Action { /** * Action called on call back from user auth step in OAuth process */ public ActionForward doPerform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // Return the session HttpSession session = request.getSession(); String tokenSecret = (String) session.getAttribute("token_secret"); cat.debug("back from Auth page - token secret:" + tokenSecret );
// Token returned from user authentication step String oauth_token = request.getParameter("oauth_token"); cat.debug("back from Auth page - auth token:" + oauth_token );
// Create a Jersey client Client client = Client.create();
// make an API call to request the access token/secret WebResource resource = client.resource(URL_ACCESS_TOKEN);
// Add the token secret to the secrets object OAuthSecrets secrets = new OAuthSecrets().consumerSecret(CONSUMER_SECRET).tokenSecret(tokenSecret);
// Set the OAuth parameters - timestamp and nonce are managed by Jersey filter // Add the auth token to parameters 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);
// make the request and print out the resulting OAuth Keys String oauthAccessKeySecret = resource.get(String.class);
cat.debug(oauthAccessKeySecret);
// Forward control to the specified success URI return (mapping.findForward("success")); }
String CONSUMER_KEY = "<your API key>"; String CONSUMER_SECRET = "<your API secret>";
/** Category definition for this class to log to log4j. */ protected Logger cat = Logger.getLogger(this.getClass());
}
public class SmugmugDAO { /* * Example method using the Smugmug credentials */ public SmugmugData getSmugmugInfo(String photoId) { //Deconstruct the key from two parts - stored as a string id_key String smugmugId = ""; String smugmugKey = "";
// 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);
// Create parame for URL MultivaluedMap<String, String> requestParams = new MultivaluedMapImpl(); requestParams.add("ImageID", smugmugId); requestParams.add("ImageKey", smugmugKey); requestParams.add("method", "smugmug.images.getInfo");
// Extract the date from the Json string - into // a java POJO SmugmugData smugmugPhotoData = new SmugmugData(); // code omitted to extract data from Json response
// Now get comments requestParams = new MultivaluedMapImpl(); requestParams.add("ImageID", smugmugId); requestParams.add("ImageKey", smugmugKey); requestParams.add("method", "smugmug.images.comments.get");
//Need to refresh nonce explicitly - timestamp will be rolled forward by Jersey but not a new nonce params.setNonce();