2
votes

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

1
what if my request URL is not on my current domain? - Fahim Akhter
If it's not your current domain, it means you have no permission to access their resources. In this case, you need to do some hack methods to inline modify the crossdomain.xml. - Hao Nguyen

1 Answers

7
votes

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);