开发者

Can't access YouTube API from Adobe Air (using JavaScript)

I'm really new to Adobe Air, and I'm trying to get a list of YouTube videos by a specific user using the YouTube API through JavaScript.

I've tried several examples that all seem to work great when I simply click on it (inside Aptana Studio) and Run as a JavaScript Web Application.

As soon as I try to run the same thing as an Adobe Air Applicat开发者_StackOverflowion, I don't get any data. Why is that? I don't know if there's something very obvious that I'm overlooking or what.

This is what I was looking at most recently: http://code.google.com/apis/youtube/2.0/developers_guide_json.html

Can someone point me in the right direction or tell me why this isn't working in Adobe Air?


Update: OK, here's how I got it working, for anybody visiting from 2019 (tip of the hat to http://xkcd.com/979/):

<?xml version="1.0" encoding="utf-8"?>
<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml" width="100%" height="100%"
    backgroundColor="#000000" verticalAlign="middle" horizontalAlign="center"
    creationComplete="init()" borderStyle="solid">

<mx:Script>
<![CDATA[
    import mx.core.UIComponent;

    public var html : HTMLLoader = null;
    protected var url : String  = null;


    protected function init()
    {
        /* Your embedded html with all the youtube REST calls is in the "local"
           subdirectory of the application directory. Note the relation between
           the local url, the sandbox root and the remote url. */
        var localUrl : String = "app:/local/yt.html";
        var sandboxRoot : String = "http://youtube.com/";
        var remoteUrl : String = "http://youtube.com/local/yt.html";

        html = new HTMLLoader();
        html.addEventListener(Event.COMPLETE, htmlLoaded);

        /* Embed your youtube html in an iframe that is loaded dynamically. You
           could also just have this html code in a separate html file and load
           it through html.load() instead of html.loadString(). */
        var htmlString : String = "<html>"
            + "<body><center>"
            +"<iframe width='100%' height='100%' src='" +remoteUrl
                +"' sandboxRoot='"+sandboxRoot+"' documentRoot='app:/' "
                +" allowCrossDomainXHR='true'" /* May not be necessary */
                +" id='yt' name='yt'></iframe>"
            + "</center></body></html>";

        /* The next line is needed to allow the REST API in the embedded html 
           to call home when loading through loadString. */
        html.placeLoadStringContentInApplicationSandbox = true; 
        html.loadString(htmlString);
    }

    protected function htmlLoaded(e:Event):void
    {
        html.removeEventListener(Event.COMPLETE, htmlLoaded);
        frame.addChild(html);
        trace("html load complete.");
    }
]]>
</mx:Script>
<mx:UIComponent id="frame" width="100%" height="100%" visible="true"/>
</mx:HBox>

Note that this is a cleaned up version of my code and I haven't tried running it, so it may not work out of the box, but that's essentially it.


I believe the reason is that AIR does not allow external JavaScript files to be loaded:

http://help.adobe.com/en_US/AIR/1.5/devappshtml/WS5b3ccc516d4fbf351e63e3d118666ade46-7f0e.html#WS5b3ccc516d4fbf351e63e3d118666ade46-7ef7

I guess one option is to muck about with their cross-scripting and sandboxing stuff...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜