Discussions (2,332)

Greasemonkey EASY authenticated API call

view profile

mortimer? says:

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){
self.displayResultsGroups(responseText,false);
// do whatever you want here....
}
};

unsafeWindow.F.API.callMethod('flickr.groups.pools.getPhotos', {
per_page: 24,
extras: 'date_upload,icon_server',
page:page,
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.
3:51AM, 29 May 2006 PDT (permalink)

view photostream

steeev is a group administrator steeev says:

this is really great, well done with your research, will have to try it out, thanks! :)
96 months ago (permalink)

nils_asdf [deleted] says:

yes, this is great! do you have a script based on this in the wild already?
96 months ago (permalink)

view photostream

mortimer? says:

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.
96 months ago (permalink)

view photostream

mortimer? says:

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:
try{
var rsp = responseText.replace(/jsonFlickrApi\(/,'');
rsp = eval('('+rsp);
if(rsp.stat == 'ok') {
//DO SOMETHING WITH RSP
} else
GM_log("Error2 "+responseText);
} catch (e) {
GM_log("Error1 "+responseText);
GM_log(e);
}
83 months ago (permalink)

view photostream

s*ong says:

Very informative. I tried this on the flickr.photos.comments.getList 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?
75 months ago (permalink)

view photostream

s*ong says:

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.
Originally posted 75 months ago. (permalink)
s*ong edited this topic 75 months ago.

view photostream

mortimer? says:

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 :(
75 months ago (permalink)

Laura | Fotografía [deleted] says:

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.
75 months ago (permalink)

view photostream

mortimer? says:

sorry, wrong thread, you are either looking for:
www.flickr.com/groups/flickrhacks/discuss/721575941449636...
or
bighugelabs.com/flickr/poolcleaner.php
75 months ago (permalink)

view photostream

steeev is a group administrator steeev says:

theres a good description of how to handle the JSON response in the API docs:
www.flickr.com/services/api/response.json.html

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

eg

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

eval(responseText);

saves all that annoying xml parsing, cool stuff! :)
Originally posted 72 months ago. (permalink)
steeev (a group admin) edited this topic 72 months ago.

Would you like to comment?

Sign up for a free account, or sign in (if you're already a member).