flickr authorization through processing
in
Share your Work
•
1 years ago
Hi all,
I'm posting this code here because I've been wanting to do this for a long time and had a very hard time figuring out how to get it to work. Hopefully, this will save others some time... It walks you through the steps needed to let a flickr user using your app tell flickr to authorize your app. Before you start, you need to create an app in flickr and paste the proper key and secret in the pde. After that, run the sketch. If you follow the printed directions, you should be good.
- // -Log into flicker
- // -Get an API key for a new app
- // -Set the following variables appropriately
- String SECRET = "paste the secret here";
- String KEY = "paste the key here";
- // Authorization variables
- String frob;
- String token = "paste the token you get from the first run here";
- String search = "cars";
- // Array list to hold flickr image URLs
- ArrayList pix;
- void setup() {
- size(300, 300);
- background(0);
- smooth();
- pix = new ArrayList();
- println("If you need to go through the authorization process, press \"r\" to request a frob and print out the URL to the proper flickr page");
- println("If your \"token\" variable is already set from pasting the value from previous runs of the sketch, press \"g\" to start the slide show\n");
- noLoop();
- }
- void draw() {
- background(0);
- // if we have pictures to display
- if (pix.size()>0) {
- String pixURL = (String) pix.get(0);
- pix.remove(0);
- PImage img = loadImage(pixURL);
- float x0=0, x1=width, y0=0, y1=width, ratio=img.width/float (img.height);
- if (false) {
- // fit smaller size
- if (img.width>img.height) {
- x0 = -(width-width/ratio)/2.0;
- x1 = width*ratio;
- }
- else {
- y0 = -(width-width*ratio)/2.0;
- y1 = width/ratio;
- }
- }
- else {
- // fit wider size
- if (img.width>img.height) {
- y0 = (width-(width/ratio))/2;
- y1 = width/ratio;
- }
- else {
- x0 = (width-(width*ratio))/2;
- x1 = width*ratio;
- }
- }
- image(img, x0, y0, x1, y1);
- }
- }
- void keyPressed() {
- //Start the authorization process
- if (key =='r') {
- print("Requesting a frob\n...");
- frob = getFrob();
- println("DONE! frob is\n"+frob);
- print("\nBuilding authorization URL from frob\n... ");
- String authorizationURL = directFlickrToAskPermission(frob);
- println("DONE! URL is:\n"+authorizationURL);
- println("\nCut and paste the URL into your browser to authorize this app to talk to flick");
- println("When you have authorized this app in flickr, hit the \"t\" key to request a token");
- }
- // Second part, after the authorization in flickr
- else if (key == 't') {
- print("\nRequesting a token\n... ");
- token = getToken(frob);
- println("DONE! token is: \n"+token);
- println("\nIf you want to skip authorization next time you run the program,");
- println("Copy the token string and paste it into the \"token\" variable declaration");
- println("of this sketch.");
- println("\nPress \"g\" to start the slide show");
- }
- // Strat the slide show
- else if (key == 'g'){
- groupSearch(search, token);
- loop();
- redraw();
- }
- }
- String getHashString(String s) {
- byte[] md5hash;
- try {
- java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
- md.update(s.getBytes());
- md5hash = md.digest();
- }
- catch(java.security.NoSuchAlgorithmException e) {
- println(e.getMessage());
- return null;
- }
- String hS = "";
- for (int i=0; i<md5hash.length; i++) hS+=(hex(md5hash[i], 2));
- return (hS.toLowerCase());
- }
- // this program asks flicker to request permission
- String directFlickrToAskPermission(String _frob) {
- HashMap params = new HashMap();
- params.put("perms", "read");
- params.put("api_key", KEY);
- params.put("frob", _frob);
- String url = buildAuthURL(params)+calcSig( SECRET, params);
- return (url);
- }
- String getFrob() {
- HashMap params = new HashMap();
- params.put("method", "flickr.auth.getFrob");
- params.put("api_key", KEY);
- params.put("format", "rest");
- String url = buildURL(params)+calcSig(SECRET, params);
- XMLElement xml = new XMLElement(this, url);
- String frob = xml.getChild(0).getContent();
- if (frob == null) {
- println(xml);
- }
- return (frob);
- }
- String getToken(String frob) {
- HashMap params = new HashMap();
- params.put("method", "flickr.auth.getToken");
- params.put("api_key", KEY);
- params.put("frob", frob);
- String url = buildURL(params)+calcSig(SECRET, params);
- XMLElement xml = new XMLElement(this, url);
- String _token = xml.getChild(0).getChild(0).getContent();
- return _token;
- }
- // returns
- void groupSearch(String searchTerm, String token) {
- HashMap params = new HashMap();
- params.put("method", "flickr.groups.search");
- params.put("text", searchTerm);
- params.put("per_page", "5");
- params.put("api_key", KEY);
- params.put("auth_token", token);
- String url = buildURL(params)+calcSig(SECRET, params);
- XMLElement xml = new XMLElement(this, url);
- int groupCount = xml.getChild(0).getChildCount();
- for (int i=0; i<groupCount; i++) {
- println("\ngetting photos from "+xml.getChild(0).getChild(i).getString("nsid")+" "+xml.getChild(0).getChild(i).getString("name"));
- groupsPoolsGetPhotos(xml.getChild(0).getChild(i).getString("nsid"), token);
- }
- }
- void groupsPoolsGetPhotos(String groupID, String token) {
- HashMap params = new HashMap();
- params.put("method", "flickr.groups.pools.getPhotos");
- params.put("group_id", groupID);
- params.put("api_key", KEY);
- params.put("auth_token", token);
- String url = buildURL(params)+calcSig(SECRET, params);
- XMLElement xmlPool = new XMLElement(this, url);
- println(xmlPool.getChild(0).getChildCount());
- for (int i=0;i<xmlPool.getChild(0).getChildCount(); i++) {
- pix.add(getImageFromElement(xmlPool.getChild(0).getChild(i)));
- }
- }
- String getImageFromElement(XMLElement e) {
- String fm = e.getStringAttribute("farm");
- String sv = e.getStringAttribute("server");
- String id = e.getStringAttribute("id");
- String sc = e.getStringAttribute("secret");
- String url = "http://farm"+fm+".static.flickr.com/"+ sv + "/" + id + "_" + sc + "_z.jpg";
- return url;
- }
- // calc signature from secret and hash map of params and values
- String calcSig(String secret, HashMap params) {
- String sig = secret;
- ArrayList keys = new ArrayList(params.keySet());
- Collections.sort(keys);
- for (int i=0; i<keys.size();i++) {
- String k = (String) keys.get(i);
- sig += (k+(String) params.get(k));
- }
- return getHashString(sig);
- }
- String buildURL(HashMap params) {
- String url="http://flickr.com/services/rest?";
- ArrayList keys = new ArrayList(params.keySet());
- Collections.sort(keys);
- for (int i=0; i<keys.size();i++) {
- String k = (String) keys.get(i);
- url += (k+"="+(String) params.get(k));
- url += "&";
- }
- url += "api_sig=";
- return url;
- }
- String buildAuthURL(HashMap params) {
- String url="http://flickr.com/services/auth?";
- ArrayList keys = new ArrayList(params.keySet());
- Collections.sort(keys);
- for (int i=0; i<keys.size();i++) {
- String k = (String) keys.get(i);chrom
- url += (k+"="+(String) params.get(k));
- url += "&";
- }
- url += "api_sig=";
- return url;
- }