mortimer? 10:51am, 29 May 2006
Ok, here is a little "hack" for all you greasemonkey coders that are looking for a way to make flickr api calls with authentication (send to group, tag photo, etc..).

There was the method introduced by Latchie (for the batch page enhancer) that I improved (for the group page enhancer) that handles all the authentication stuff for you, but needs the user to go back and forth to allow your own API key to do stuff. This one can be used easily, but it requires:
1- interaction with the user
2- a lot of GM_xmlhttprequest
3- to store values, etc...

it's therefore a "complex" code behind the scene, prone to bugs and highly depend to the flickr page design (for interaction with the user).

But you can do all that far more easily and get the READ and WRITE permissions to the user (if logged) account directly. After all, Flickr is already doing it when you add tags, upload new files, etc... isn't it.

So, here is a code snippet to show you how to make Flickr API call by using the flickr javascript directly:

var self = this;
var listener = {
flickr_groups_pools_getPhotos_onLoad: function(success, responseXML, responseText, params){
// do whatever you want here....

unsafeWindow.F.API.callMethod('flickr.groups.pools.getPhotos', {
per_page: 24,
extras: 'date_upload,icon_server',
tags: document.getElementById("standard_q").value,
group_id: this.groupID
}, listener);

In summary, you have to call the function unsafeWindow.F.API.callMethod with the following parameter:
- the API method
- an object representing the additional parameters. NB:
o the api_key, user_id, auth_token, sig etc... are already provided by flickr, you do not need to include them in there,
o the parameters are encoded by the flickr javascript lib, so no need to call encodeURIComponent on form values
- a listener object.

The last parameter, the listener object is an object with the callbacks function that should be called when the call returns. It has to have properties with the right names pointing to handler functions.

The listener property name are constructed as follow:
- the API method with the . replaced by _
- the xmlhttprequest event to listen to: onLoad for example
So, for the onLoad event of the flickr.groups.pools.getPhotos method, the listener is:
{flickr_groups_pools_getPhotos_onLoad: function(success, responseXML, responseText, params){...}}

The handle functions takes the arguments:
- success: has the API method call succeeded (was stat="ok" in the returned xml)
- responseXML, the xml document returned if any
- responseText, the raw text of the response
- params, the list of params sent to the API call (include the params you provided earlier and the sig, api_key, etc...)

Flickr API calls are now really easy to make this way from GM scripts. I hope that flickr people will not be too mad if we use it directly this way.
steeev 11 years ago
this is really great, well done with your research, will have to try it out, thanks! :)
nils_asdf [deleted] 11 years ago
yes, this is great! do you have a script based on this in the wild already?
mortimer? 11 years ago
Flickr Gamma More Search uses this hack to do the search. So it's only read permission it's using, I haven't tried to write things with it yet.

I will soon try, with improvements to the "Flickr group display" script to use the write rights and see what happens.
mortimer? 10 years ago
if you give the parameter: format=json when doing the query, you'll get an easily parsable object in javascript.
You can then use the following code to construct the object:
var rsp = responseText.replace(/jsonFlickrApi\(/,'');
rsp = eval('('+rsp);
if(rsp.stat == 'ok') {
} else
GM_log("Error2 "+responseText);
} catch (e) {
GM_log("Error1 "+responseText);
s*ong 9 years ago
Very informative. I tried this on the API, I have a onLoad listener is place, and I track the success status. Work for the most part. Occasionally, I get this:

"Error: unexpected end of XML entity"

I'm pretty sure the onLoad is not being called, I suspect I need a onError, but I don't have the method signature, do you have an example?
s*ong Posted 9 years ago. Edited by s*ong (member) 9 years ago
I found a solution with help from JPO. I'm throwing this in here in case it help someone else.

In my onLoad listener, I was turning the responseText into XML object.

var listener = {
flickr_photo_comments_getList_onLoad: function(success,responseXML, responseText, params) {
var xml = new XML(responseText);

Because the photo comments comes from the end users, it can contain string sequences that becomes illegal in XML, this is most often from those mandatory group badges

The solution is to surround the XML objection creation in try/catch. This allow the script to actively handle the situation. Unfortunately, for photo comments, this work around is very course, on error, it means ALL the comments for a particular photo cannot be processed. It would have been nice if Flickr trap the bad comment sequence from forming bad XML to begin with.

Alternately, Mortimer suggested using JSON instead of XML. I havent't tried it, but it sounds promising.
mortimer? 9 years ago
Using JSON would be faster to process on the clientside I think, and it might help make the GM script compatible with other user scripts enabled browsers as not many browsers support the E4X XML object :(
Laura | Fotografía [deleted] 9 years ago
is this the script that allows photos in a group pool to disappear with a puff of smoke instead of having to click "OK" and reload the page everytime? Someone on flickr Ideas told me about this, and I'm looking for scripts that will help me moderate my group better.
mortimer? 9 years ago
steeev Posted 9 years ago. Edited by steeev (admin) 9 years ago
theres a good description of how to handle the JSON response in the API docs:

they say you should define the "jsonFlickrApi" function yourself, then you just need to call the function by evaluating the responsetext


jsonFlickrApi = function (rsp) {
alert('return status=' + rsp.stat )


saves all that annoying xml parsing, cool stuff! :)
Groups Beta