Search code examples
c#youtube-apiyoutube.net-api

Youtube API - 403 Forbidden: invalid developer key for a simple Get request


I'm just starting with the Youtube API and I'm facing something that I cannot explain. Basically, I just need to retrieve videos from Youtube and I'm pretty sure that you do not need to be authenticated for that (if not, please tell me). After going through the API doc, I have this very basic request: http://gdata.youtube.com/feeds/api/videos?q=ted+talks&max-results=10&v=2 (searching for "ted talks" and taking the first 10).

Doing this request directly in the browser or via an http request works which confirms my belief that there is no need to be authenticated for that.

But here is the thing, I would like to use the .NET client library that Google provides to avoid all the XML handling/deserialization and here is my code:

public string Test()
    {
        YouTubeRequestSettings settings = new YouTubeRequestSettings("Otello", String.Empty);
        YouTubeRequest request = new YouTubeRequest(settings);
        Feed<Video> feed = request.Get<Video>(new Uri("http://gdata.youtube.com/feeds/api/videos?q=ted+talks&max-results=10&v=2"));
        return feed.Entries.Count() + "";
    }

This code always returns a 403 Forbidden error (invalid developer key). Am I doing something wrong? Do I really need a developer key for this kind of calls? And if I do, I tried to go there https://code.google.com/apis/youtube/dashboard/gwt/index.html#settings but I cannot add a new product in the following page.


Solution

  • I don't think you need a developer key. I tested your url (using Json.Net)as below and it works

    WebClient wc = new WebClient();
    string resultStr = wc.DownloadString("http://gdata.youtube.com/feeds/api/videos?q=ted+talks&max-results=10&v=2&alt=jsonc");
    dynamic result = JsonConvert.DeserializeObject(resultStr);
    foreach (var item in result.data.items)
    {
        Console.WriteLine("{0}: {1}",item.title,item.player["default"]);
    }
    

    PS: I appended &alt=jsonc to get the result as json. You can remove it and get the result as xml.