JSONP and Callback
bmsterling
Registered Users Posts: 28 Big grins
Hey all,
Do to the fact that a good amount of hosting providers have "allow_url_fopen" set to off, I need to implement an alternate way to get the users photos for the WordPress plugin I built (http://benjaminsterling.com/photoxhibit/).
I am going to implement something like what I have here (http://benjaminsterling.com/jquery-jqalbumparser-plugin-parses-out-flickr-picasa-clientside/) but I have two problems/question when it comes to dealing with SmugMugs API.
First question/problem is is there a way to grab the photos without setting a session id first? Doing two JSONP calls to grab the photos does not make a whole lot of sense and will be slightly challenging in the currently set up of the plugin.
Second, how do I implement the callback? I saw on another post that there is a callback but I don't see it in the API.
Thanks.
Do to the fact that a good amount of hosting providers have "allow_url_fopen" set to off, I need to implement an alternate way to get the users photos for the WordPress plugin I built (http://benjaminsterling.com/photoxhibit/).
I am going to implement something like what I have here (http://benjaminsterling.com/jquery-jqalbumparser-plugin-parses-out-flickr-picasa-clientside/) but I have two problems/question when it comes to dealing with SmugMugs API.
First question/problem is is there a way to grab the photos without setting a session id first? Doing two JSONP calls to grab the photos does not make a whole lot of sense and will be slightly challenging in the currently set up of the plugin.
Second, how do I implement the callback? I saw on another post that there is a callback but I don't see it in the API.
Thanks.
0
Comments
I've been kicking around doing the same thing on and off now. I actually used your albumparser for a template when I started off, but quickly found that it was limited in use for smugmug for exactly the reason you stated.
You need to make at least 2 calls (I think 3 calls now with the addl security...) and I had a hell of a time making sure that the ajax calls are done sequentially since there are no wait statements in javascript. Seems a pretty good bet that you have to use nested ajax calls in jQuery.
Use jQuery JSONP: http://docs.jquery.com/Ajax/jQuery.getJSON#urldatacallback, it seems the callback has to be exactly: "&JSONCallback=" and jQuery seems to understand the JSONP callback as of 1.2.x. It will be automatically called if the callback ends in "=?", so the full URL must be:
http://api.smugmug.com/services/api/json/1.2.1/?method=smugmug.login.anonymously&APIKey=YepThisIsMyKey&JSONCallback=?
Or more generically:
URL: http/api.smugmug.com/services/api/json/1.2.1/
method: ?method=smugmug.xxmethodnamexx
APIKey: &APIKey=YepThisIsMyKey
Callback: &JSONCallback=?
This seems to access the sessionID nicely:
******** type="text/javascript">
$(document).ready(function(){
$.getJSON("http://api.smugmug.com/services/api/json/1.2.1/?method=smugmug.login.anonymously&APIKey=YepThisIsMyKey&JSONCallback=?",
function(data){
sessionID = data.Login.Session.id;
alert(sessionID)
});
});
</********
If you are interested I wouldn't mind sharing the code I've come up with so far.
That is exactly what I was planning on doing, just did not want to have to create code specifically for SmugMug more then I already have. Not entirely sure why the need for extra security if it is going to be a JSONP call. If there is worry about not so nice people getting a hold of someone hi-res images via JSONP maybe putting a switch that will only give up the 800 px wide and a smaller thumbnail.
But if you don't me sending me over what you have, I would like to at least take a look at your approach.
http://KenzoMedia.com
http://KenzoHosting.com
http://benjaminsterling.com
Sorry for the slow answer, but here goes the solution I came up with:
http://pastebin.com/m13c5919c
I hope it may be helpfull. I did try to avoid inner loops but getting ajax calls sequential is beyond me.
I think you only need 1 SessionID per page, therefore only 1 call per page for the sessionID and then the rest is done in the inner loop.
The plugin is still crued it does not extract the albumID from the link yet, but you can pass the Album ID directly or using the metadata plugin.
Cheers
I have it in my newest release and it is working well; I agree with you, I prefer not to nest, but if it work, it works. Now I just need to figure out how get this plugin to play nicely with everyones server.
Thanks again for your help; glad to see another jQuery guy out in the wild.
http://KenzoMedia.com
http://KenzoHosting.com
http://benjaminsterling.com