Category Archives: Flickr

Let’s say we want to create a test script testflickr.php, which will get access to user’s Flickr account and show some information.

  1. Get a Flickr API key and secret (http://www.flickr.com/services/apps/).
  2. Set the callback URL for the new app (click your app name and then “edit” in the right menu). This can be the test file you are creating, so http://…/testflickr.php.
  3. testflickr.php can look like:


<?php
require_once("phpFlickr/phpFlickr.php");
$api_key = your Flickr app key;
$api_secret = your Flickr app secret;
$f = new phpFlickr($api_key, $api_secret);
if (empty($_GET['frob'])) {
$f->auth("read", false);
} else {
$f->auth_getToken($_GET['frob']);
}
$token = $f->auth_checkToken();
$nsid = $token['user']['nsid'];
// Get the friendly URL of the user's photos
$photos_url = $f->urls_getUserPhotos($nsid);

Flickr photo hosting service provides an extensive API allowing for full control over the image files, tags, contacts, notes etc. Language-specific API’s are available for many programming languages (see http://www.flickr.com/services/api/). In a small tutorial (http://www.mathworks.com/matlabcentral/fileexchange/34162) I have shown how to use Flickr API in Matlab.

There are a few tricks to use when developing an app using Flickr API. They are not nicely described by Flickr (as of December 2011) and it takes some effort to find out how to do some simple things. Here I will be collecting the solutions.

I’d like to thank Sam Judson’s for help on the way to coping with Flickr’s OAuth. More information on Flickr OAuth can be found in his tutorial: http://www.wackylabs.net/2011/12/oauth-and-flickr-part-1/.


  1. Callback value for stand-alone applications: oob, mentioned here.
  2. When using urlread function of Matlab, in case something did not work as expected, Matlab shows a rather useless error message “Error using urlread (line 113). Error downloading URL. Your network connection may be down or your proxy settings improperly configured.” This is actually not at all what happens. In order to see what actually happens, paste the request line generated by the code (passed to urlread function) to the browser. Flickr will give a response explaining what it does not like, e.g.
    oauth_problem=timestamp_refused (check that your time is the Unix epoch time, in seconds, and that it’s UTC, not your local time),
    oauth_problem=nonce_used (each request to Flickr should have a unique random string identifier), or oauth_problem=signature_invalid&debug_sbs=GET&http%3A%2F%2Fwww.flickr.com...
  3. If you get the signature invalid error, it means that your request is generally correct but the signature is wrong. This is a tough problem since any tiny error in the base string algorithm will completely spoil the signature. To help resolving this issue, Flickr provides its own version of the base string, which you can compare to yours. I usually compare them in a Notepad by pasting the two strings one below another. The rules of building the base string are explained by Flickr and also in my tutorial.
  4. OAuth signature looks like a random string so it’s hard to debug. Therefore before searching for other problems in your code, make sure that your signature generation algorithm works correctly. You can compare it to this online signature generator: http://hash.online-convert.com/sha1-generator.
  5. Base string to be signed should be url-safe. Matlab provides function urlencode. Beware: it should be followed by
    s = strrep(s, '+', '%20')
    because of two versions of such encoding. See http://www.albionresearch.com/misc/urlencode.php
  6. XML response received from Flickr may be read using Matlab XmlDOM functionality. I have a short separate post on it.
  7. Alternatively, one can obtain the response in the JSON format, using two additional parameters: format=json and nojsoncallback = 1.