YouTube GData API returning error 400 when uploading a video

I’ve been battling with the YouTube API for the last 2 days now and found the single line of code that has been preventing me from generating a form upload token.

I was following the example code at http://code.google.com/apis/youtube/developers_guide_php.html#UploadingVideos and kept receiving “Expected response code 200, got 400″ when calling the getFormUploadToken function. HTTP error code 400 is a bad request which wasn’t really much help.

Eventually I found the problem and I will own up straight away that it was basically my fault, I’d made what I considered to be a slight change to the example code, I changed

$mediaGroup->category = array(  $yt->newMediaCategory()->setText('Autos')->setScheme
('http://gdata.youtube.com/schemas/2007/categories.cat'),

$yt->newMediaCategory()->setText('mydevelopertag')->setScheme
('http://gdata.youtube.com/schemas/2007/developertags.cat'),

$yt->newMediaCategory()->setText('anotherdevelopertag')->setScheme
('http://gdata.youtube.com/schemas/2007/developertags.cat')

);

to the following

$mediaGroup->category = $yt->newMediaCategory()->setText('Autos')->setScheme
('http://gdata.youtube.com/schemas/2007/categories.cat');

because I wasn’t interested in multiple categories, just something simple. My mistake was not in removing the two developer tags but removing the array(). This took me almost an entire day to realise and resulted in a “You have to be kidding” comment from me once fixed. *Sigh* oh well, not going to get that time back but at least I learned not to mess with example code.