2

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

St. John Johnson
  • 6,590
  • 7
  • 35
  • 56
  • what if my request URL is not on my current domain? – Fahim Akhter Jan 21 '10 at 20:15
  • 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 Jul 05 '17 at 05:30

1 Answers1

7

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);
Jacob
  • 77,566
  • 24
  • 149
  • 228
  • Just tried that, new error: Error #2044: Unhandled SecurityErrorEvent:. text=Error #2048: Security sandbox violation: [...] cannot load data from http://en.gravatar.com/avatar/97fbce86a5bbc520450168603172cd9e?s=35&d=identicon. – St. John Johnson Jul 07 '09 at 12:34
  • 1
    Okay, this solved my problem! Don't add the SecurityDomain though, it only makes things worse. Also, you cannot directly access the content of the Loader, that throws another error. – St. John Johnson Jul 07 '09 at 13:18
  • @John how should you access the content then? – cregox Jun 27 '11 at 04:12