Flash Crossdomain.xml Problems

St. John Johnson picture St. John Johnson · Jul 6, 2009 · Viewed 9.9k times · Source

I'm trying to load Gravatars into Flash. Luckily, they provided a crossdomain.xml file at http://en.gravatar.com/avatar/crossdomain.xml

My code:

Security.loadPolicyFile("http://en.gravatar.com/avatar/crossdomain.xml");
var loader:Loader = new Loader();
loader.load(new URLRequest("http://en.gravatar.com/avatar/" + gravatar + "?s=35&d=identicon"));

But I'm still getting this error:

SecurityError: Error #2123: Security sandbox violation: LoaderInfo.content: [...] cannot access http://en.gravatar.com/avatar/97fbce86a5bbc520450168603172cd9e?s=35&d=identicon. No policy files granted access.
at flash.display::LoaderInfo/get content()
at PiecePlayerSmall/onLoadComplete()

I also monitored the traffic the Flash file is sending. It's requesting:

Any suggestions for getting this to work and reducing the number of requests to gravatar.com.

EDIT: The following code works, thanks to Jacob

Security.loadPolicyFile("http://en.gravatar.com/avatar/crossdomain.xml");
var context:LoaderContext = new LoaderContext();
context.checkPolicyFile = true;
context.applicationDomain = ApplicationDomain.currentDomain;
var request:URLRequest = new URLRequest(
    "http://en.gravatar.com/avatar/" + gravatar + "?s=35&d=identicon");
var loader:Loader = new Loader();
loader.load(request, context);
this.addChild(loader);

Note: Do not try to access the content directly in the Event.COMPLETE

Answer

Jacob picture Jacob · Jul 7, 2009

I got around a similar issue by using a LoaderContext. Here's an example of how to do this:

var context:LoaderContext = new LoaderContext();
context.checkPolicyFile = true;
context.securityDomain = SecurityDomain.currentDomain;
context.applicationDomain = ApplicationDomain.currentDomain;
var request:URLRequest = new URLRequest(
    "http://en.gravatar.com/avatar/" + gravatar + "?s=35&d=identicon");
var loader:Loader = new Loader();
loader.load(request, context);