Youtube Data API AS3 – First Tutorial

Hi everyone,

In this tutorial I will demonstrate how to use the YouTube Data API AS3 Library to:

  • search for videos using a keywords, categories and tags;
  • demonstrate how to distinguish between feeds returned to you by the API by using the requestId;
  • get information from the result feed, in this case a VideoFeed object. We will get the video, it’s main URL, then go through the different thumbnails available for a specific video.

So let’s get started

So far we’ve defined our class, SearchSample, and two member variables: _ws (which will contain a reference to the YouTubeClient singleton) and _requestId (to store the unique id associated with our specific request).

Next we will get a reference to the YouTubeClient class, listen for the proper event, launch our search request and store the requestId associated with it.

Now on with the event handler, doSearchResults. We will:

  • make sure we are treating the right request, since YouTubeClient is a singleton, this can become important when you have multiple calls being made from different places in your program.
  • get a reference to the VideoFeed object contained inside of the VideoFeedEvent object passed as a parameter to the event handler.
  • create a variable to contain the VideoData objects describing the videos received in the results
  • iterate through the first set of results (since we are getting the first 50 by default
  • see some useful variables such as the Title of the video and it’s author’s name
  • get a reference to the ThumbnailIterator which will give us access to all available thumbnails
  • create a variable to contain ThumbnailData objects which describe each thumbnail available

That’s it folks! The project is now available at Google Code, here: http://code.google.com/p/as3-youtube-data-api/

I could use help with documentation, if anybody feels up to the task, leave your email or contact info in a comment!

19 thoughts on “Youtube Data API AS3 – First Tutorial”

  1. Getting the following error on doLogin:
    WSClient.doHttpStatus:200
    doLogin
    ArgumentError: Error #2096: The HTTP request header Authorization cannot be set via ActionScript.
    at flash.net::URLStream/load()
    at flash.net::URLLoader/load()
    at ca.newcommerce.youtube.webservice::YouTubeClient/runLoader()
    at ca.newcommerce.youtube.webservice::YouTubeClient/rate()
    at ca.newcommerce.youtube::Test/doLoggedIn()
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at ca.newcommerce.youtube.webservice::YouTubeClient/doLogin()
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at flash.net::URLLoader/onComplete()

    Can you advise please? Thanks

  2. Alan,

    I am working on a version which uses a AMFPHP Proxy. This problem you had is caused by the security sandbox from the Flash Player, by using a proxy we have complete freedom. In PHP and most other programming languages you don’t have these (stupid) limitations. I use cURL to make the requests, and return the responses using AMFPHP. It works quite nice. I recently posted on it. The API will be updated shortly.

  3. Pedro,

    this problem is due to the crossdomain.xml policy file found on i.ytimg.com, over which I have no control. I suggest posting this comment in the google group on the Youtube Data API developer forum.

  4. Hi,

    i really liked this AS3 Classes, but i can´t download from code.google…there is a problem? -

  5. Hello,
    I’m a AS3 new beginner and I’ve read your sample code in this article. But when I press Ctrl + Enter to test, it show: “Error:5000, it must have sub-class “flash.display.MovieClip”
    Then I import flash.display.MovieClip, but still pop the same error code 5000. Could you kindly give me some suggestions?
    Thanks a lot!!

  6. Hi,

    I tried to use the method getVideo from YouTubeFeedClient class, but I have the next message:

    WSClient.doHttpStatus:200
    doVideoLoaded

    The code it is the follow:

    _ws = YouTubeClient.getInstance();
    _ws.addEventListener(VideoFeedEvent.VIDEO_DATA_RECEIVED, doSearchResults);

    _requestId = _ws.getVideos(“uEGPF1be6RM”);

    anyone know, what happend?

    Reagards.

  7. Hi!
    Impressive work indeed, and its really amazing the kind of interaction with Youtube you can acchieve.

    I am having a really annoying problem though, where amidst all the data can I retrive the URL to the netstream I need to connect my Video player to actually play the stream?

    TIA
    Niklas Wörmann
    Sweden

  8. Okay…I think I must be a complete idiot, cause I’m really having a hard time getting this. (Though I’m making progress)

    I’m trying to get a the ten most recently uploaded videos from a particular youtube account, and I just can’t seem to make it happen.
    (I think I need to use “VideoFeedEvent.VIDEO_DATA_RECEIVED”, but I’m not sure how to do it…)

    So far, this is the most mack-daddy site that I’ve found for the youtube API. Can somebody please help me? PLEASE!!!

  9. Okay…I figured it out. The latest download (0.95) gets a little confused, because the test files and classes are trying to access YouTubeClient.as and it doesn’t exist anymore. (They are now YouTubeDataClient.as & YouTubeFeedClient.as)

    Simply switch it to the most appropriate file and you’ll be golden.

    Now, if I can just figure out how to attach my new content to items on my stage I’ll be crazy happy!

  10. Two days later…still trying to figure out the getAt method. ARGH!!!

    I think that’s how I need to call specific lines from the feed.

  11. I found these issues when I am using this api to our project,can any one help me:

    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at ca.newcommerce.youtube.iterators::ThumbnailIterator/get count()
    at ca.newcommerce.youtube.iterators::ThumbnailIterator/getAt()
    at ca.newcommerce.youtube.iterators::ThumbnailIterator/next()
    at ca.newcommerce.youtube::Test/doFavoritesReady()
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at ca.newcommerce.youtube.webservice::YouTubeFeedClient/doUserFavoritesLoaded()
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at flash.net::URLLoader/onComplete()

    Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">