Hey Flickr, Where Did My Statistics Go? OAuth, the Flickr API and You.   3 comments

In Yahoo’s misguided attempts to make Flickr a Phone photo sharing app like Instragram has radically redesigned the interfaces on both the desk top and on phones. Along the way they give you the option of uploading all of your phone photos directly to Flickr from your phone.  Which is nice, I guess, but Google and Microsoft and DropBox also offer the same service. I mean why would I want to dump my raw photos to Flickr when I, like most Flickr users, do our processing offline (in Photoshop for example) and post our best work to Flickr.  It would seem they are abandoning their core users (amateur photographers) to make way for the Instragram raw sewage stream of sunsets, meals and selfies.  Strange.  They are going to lose out to the Instragram / Facebook for that market and alienate the current group of core users. Is this what you want to see when you go to Flickr?  This:

selfie  Or this?  9475221248_2009bb3d34_o[1]

In order to accommodate the change Flickr, like all the big players offers a huge amount of cloud storage ‘for free’.  Something like two Gigabyte of photo storage.   Before the big changes at Flickr there was a special Flickr service “Flickr Pro” which for $29 a year got you, among other things, daily usage statistics (going back 30 days) of your photos.  Statistics were also available via the Flickr API (more on this in a moment). With the new Flickr statistics are gone and the Flickr API for statistics are also gone.

So I started digging into the existing, and still supported, Flickr API to see what was necessary to get usage statistics from Flickr for my photos using the Flickr APP.  I a C# programmer by profession so I started by looking at existing Flickr API libraries to leverage in this quest.  The godfather of C# Flickr API libraries is the FlickrNet API Library which is available in source under both the LGPL 2.1 license, and the Apache 2.0 license.

But First

This is a great and fully comprehensive library but to use it you need a couple of things to get started.  You must obtain a non-commercial personal API Key from Flickr (an application key). Apply for one here. But wait there is more.  Although the Flickr API is fairly straight forward you must, must, must, use Flickr’s implementation of OAuth with the application key to access Flickr data.  This post will be devoted to getting up and running with the Flickr API and Oauth.  Subsequent posts will discuss how to read photo view data and present a simple file database to store statistics.  Finally I will turn to using CouchDB and a Web front end to do the same work.  For this part we will switch over to Ruby and write our own simplified FLickrNet library.

OAuth

OAuth, in its many variations, is all over the place on the web these days.  Every time you see a “Sign In with…” Facebook, Google, whatever you are using OAuth in action.  You must use the Flickr OAuth implementation  to access your own data on Flickr.  It is really not all that hard although at first blush it looks impossible.  Here is Flickr’s own overview:

Simple and fun you say!

32607_alice_madness_returns

It’s not that bad when you get into it.

Using code samples provided by the folks at FlickrNet here is a simple walk through of how to setup OAuth to access your own data for a desktop application.  First we will look at it as a simple cook book approach (in C#) and then give a much simpler view of OAuth than the above diagram.  And that will be it for this post.

  • Application Key

Get an application Key and a “shared” secret from Flickr (start here).

  • Grant Access Writes for this Application Key

Get the user (that’s you to grant you access to their (your) photos.

Prepare a special HTTP  Request to Yahoo which looks like

http://www.flickr.com/services/oauth/authorize?oauth_token={hash}&perms={permsValue}

where:

{permsValue} is the access writes you are requesting to have granted to the caller

{hash} is a   Base64 encoded SHA-1 hash based on your Application Key and shared secret.  Not to worry FlickrNet handles the details for you.

Assuming you have created a FlickrManager class object (following the examples in the FlickrNet Samples Site) which includes your Application Key and shared secret the request URL is created as:

Flickr f = FlickrManager.GetInstance();  //you create this and encode your Application Key and Shared Secret into this class

requestToken = f.OAuthGetRequestToken(“oob”); //”oob” indicates that you are making a desktop request not a request from a web page

string url = f.OAuthCalculateAuthorizationUrl(requestToken.Token, AuthLevel.Read);//Request access to only what you need, in this case READ

opening this url will direct you the Yahoo login page and once logged in ask if you which to grant access for this application key to your data (photos and stuff) on Flickr.  When you answer YES Flickr will present to you on the screen (no call back as requested) an grant access key which is unique to your data and your application key.  This key is also called the“verifier” Write this down, we will use it below.

Here is what is happening in principle.  Flickr and you as the developer have a trust relationship where they grant you and application key for your use as a non-commercial developer.  The shared secret  is used as a key to encrypt messages between you (the developer) and Flickr.  This is used to request an access grant key between this application key (as a developer) and your data (as a Flickr user).  This is a one time process.

Get The OAuth Token

Each time we want to get  data back from Flickr we need to combine our Application Key, Shared secret and the access grant into a second specialized URL Get request to receive an OAUTH token which is time limited. In simple FlickrNet Code this looks like:

Flickr f = FlickrManager.GetInstance();
var accessToken = f.OAuthGetAccessToken(requestToken, verifier);
FlickrManager.OAuthToken = accessToken;

Here requestToken is a class object containing the Application Key and shared secret.  The field verifier contains the Grant Access Key we obtained above. The call to OAuthGetAccessToken builds a HTTP Get request to:

https://www.flickr.com/services/oauth/request_token?oauth_signiature={sig}

where:

seg is a SHA-1 Hash of the input parameters. Let FlickrNet handle the details for you.

Ok So Far? Here is a picture:

Picture1

 

Each data call to Flickr for data is an HTTP call and should be signed with the Application Key and the Access Object Token. Responses are returned in any of several formats of which XML and JSON are the most important.  Here is what a simple call to get photo data for a specific user (and returning XML) looks like:

https://api.flickr.com/services/rest/?method=flickr.people.getPhotos&api_key=123456789&user_id=12345&format=rest&auth_token=12345-6789&api_sig=98765

where:

api_sig is a hash of this call based on our Token Secret.  Not to worry we will use the FlickrNet assembly to generate this value.

Ok.  That’s enough for one post.  Get your Application Key from Flickr, Run the samples for FlickrNet to get your access key and generate an Access Object (OAuth Token), and in the next post we will dig in with using the Flickr API.

 

Part 2 continues here.

 

 

 

 

 

Posted 2014/07/03 by Cloud2013 in Flickr, Photography, Random Notes

Tagged with , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 122 other followers

%d bloggers like this: