How to handle the gdata is not defined error for the Google Picasa web albums API

Update: An acknowledgment of a mistake/bug was made in a post on a Google forum and indicated they are implementing a change. The change appears to have now gone through, and the previous existing functionality should now work. So, even Google makes big mistakes, which is ok, but I question why any changes at all would be made to existing API functionality. API changes should be branched out and push changes only to the new branch. That said, thanks to Google for the fix, though it took over 24 hours to get it in place, while many Picasa customers were left with broken galleries. I hope at some point Google will develop better support channels so developers are not left in the dark. I'm leaving the post up in case anyone needs to resolve this problem in the future if it happens again. 

Many users of the Picasa web albums API will have noticed that yesterday Google introduced a terrible code change in the API which has broken existing functionality for many users of the API when using JavaScript. Now, instead of a nice web album, they receive ' gdata is not defined '. Excellent job Google, what a way to celebrate the 5th birthday of Google Code by breaking your existing APIs. Supposedly, they have a fix coming, in the meantime if you are frantic to fix things for your clients, read on for a solution. 

Ok, first let's get the facts straight. 

  • There is a problem, Google finally responded on a message board this morning acknowledging the issue. So much for those of us who were up all night...
  • The problem stems from a change in Google's Web Album API and the feed it returns when doing requesting a JSON feed of an album of photos or other such feed. 
  • Specifically, if you are using json-in-script as a callback, e.g., http://picasaweb.google.com/data/feed/api/user/nowarninglabel/albumid/5401868408614943617?kind=photo&alt=json-in-script&callback=outputone&access=public then you will see a javascript error gdata is undefined. 
  • The change wraps the feed in gdata.io.handleScriptLoaded whereas before you would just get the feed. I don't know if maybe some engineer thought picasa web albums was part of the gdata client library, but it's not, there is no loadable jsapi module for picasa. 

Ok, so how can we fix this?

  1. Back up your existing code. 
  2. Basically, gdata.io.handleScriptLoaded is hijacking the callback. Normally, we could just write a function, e.g. , function gdata.io.handleScriptLoaded() in our Javascript code on the page, and then it would load code from that function. However, since there are dots in that name, JavaScript won't accept it as a function name and will give you an error about ( and parameters. I tried some funky eval() but didn't work out. 
  3. Fortunately, someone had experienced this as an unrelated issue before and had a hack of using a skeleton object to load gdata.io.handleScriptLoaded (sorry can't find the forum post again, but will link to it when I find it)
  4. Use the following code in your Javascript file, above where you would normally define your callback function:

 

 

The only thing you need to change is outputone, that you will replace with the name of your callback function. As you can see in the example URL I provided above my &callback=outputone, so that is the function I want to call.  Just for reference, here is what my outputone function looks like:

 

function outputone(data)

{

for (var e = 0; e < data.feed.entry.length; e++)

{

var item1 = data.feed.entry[e];

var title1 = item1.title.$t;// the filename

var imgsrc1 = item1.content.src; //used for downloading image

var height1 = parseInt(item1.gphoto$height.$t);

var width1 = parseInt(item1.gphoto$width.$t);

var orientation1 =(height1>width1 ? "vert" : "hor");

var description1 = item1.media$group.media$description.$t;// Picasa Web photo caption

document.write("<li><img  id='one"+e+"' class='gallery "+orientation1+" slideone"+e+"' src='" + imgsrc1 + "?imgmax=72' title='"+description1+"' onClick='enlarge(this);'></img></li>");

}

}

 

So, will this solve all of your problems? Well, if you only have one photo album on the page, then yes it should. If you have multiple albums, it probably still can but you will have to tweak things a bit. This code should be compatible with a fix if Google fixes things because we are saying if(!gdata) in other words if gdata is not defined, then do the following. However, you may want to be cautious and check back every day and make sure things are working, if not they may have fixed this regressive bug, so restore your backed up copy of your code and you will hopefully be good to go. 

Have more Javascript questions? May I suggest JavaScript: The Definitive Guide a.k.a. the Rhino Book. It is a really excellent reference and learning aid. 

 

 

Comment using an existing account (Google, Twitter, etc.)