Friday, December 28, 2012

YouTube Data API (V3) Javascript Client Example problem


Everyone knows the YouTube Data API (V3) is still experimental.  But still, i'm sure most developers are excited to try new stuff from Google.

I was eager to build an ASP.NET MVC site that would be able to upload videos to YouTube.

I tried playing around with the YouTube .NET Client API hoping it would simplify things but then I realized this was a no browser implementation and the user had to manually enter an authorization key.  Ok this is not what I need.

I tried out the YouTube Javascript Client API and followed the instructions on the Samples page and it looked pretty neat and straightforward.  Then when i tried to run the application, it didn't work! (of course, it's experimental).  

First, let me show you the code from Google:

//Retrieve the uploads playlist id.
function requestUserUploadsPlaylistId() {
    // https://developers.google.com/youtube/v3/docs/channels/list
    var request = gapi.client.youtube.channels.list({
        // mine: '' indicates that we want to retrieve the channel for the authenticated user.
        mine: '',
        part: 'contentDetails'
    });
    request.execute(function (response) {
        playlistId = response.result.items[0].relatedPlaylists.uploads;
        requestVideoPlaylist(playlistId);
    });
}

There were 2 errors i got:

1.  TypeError: response.result is undefined

When i looked at the JSON response from Google, here's what I saw:

[
 {
  "error": {
   "code": 400,
   "message": "Invalid boolean value: ''.",
   "data": [
    {
     "domain": "global",
     "reason": "invalid",
     "message": "Invalid boolean value: ''."
    }
   ]
  },
  "id": "gapiRpc"
 }
]


Then i looked at the line of code making the request:
var request = gapi.client.youtube.channels.list({
        // mine: '' indicates that we want to retrieve the channel for the authenticated user
        mine: '',
        part: 'contentDetails'
    });
And I just changed mine: '' to mine: true which went through.  But there was 
another error.

2.  TypeError: response.result is undefined (same error actually)
But happens on this line:
Looking at the response, I saw this:
[
 {
  "error": {
   "code": -32602,
   "message": "No filter selected.",
   "data": [
    {
     "domain": "youtube.parameter",
     "reason": "missingRequiredParameter",
     "message": "No filter selected.",
     "locationType": "parameter",
     "location": ""
    }
   ]
  },
  "id": "gapiRpc"
 }
]
The solution is to change this:

playlistId = response.result.items[0].contentDetails.uploads;

to

playlistId = response.result.items[0].contentDetails.relatedPlaylists.uploads;

And i'm glad i was able to retrieve my uploaded videos!!!

5 comments:

  1. Thanks! just what i needed

    ReplyDelete
  2. yup, had to do this too, glad its not just me :)

    ReplyDelete
  3. hey av changed everything as u've put it bt am getting the same error (the second error) bt now at the line I've jst changed i.e at playlistId = response.result.items[0].contentDetails.relatedPlaylists.uploads;

    ReplyDelete