Updated Solution: AS3 Security Error #2122 with 300 Redirects


Posted on December 28th, by Mark in Flash. 2 comments

Quite a while ago Steven Sacks posted a solution to 300 redirects when loading images in Flash.
This seemed to work most of the time but the solution does not work every time (at least anymore).
One thing I noticed with Facebook in particular is that the contentLoaderInfo.url (the redirected url) was not a complete url with profile images. So loading that url would not work.

Started looking into other possible solutions and low-and-behold, the language reference actually tells you what to do.
At the bottom of the checkPolicyFile section, Adobe mentions steps to take with redirects:

  1. Examine the value of LoaderInfo.url after you have received a ProgressEvent.PROGRESS or Event.COMPLETE event, which tells you the object’s final URL.
  2. Call the Security.loadPolicyFile() method with a policy file URL based on the object’s final URL.
  3. Poll the value of LoaderInfo.childAllowsParent until it becomes true.

I put together some example source code below to walk you through the steps.

private var loader:Loader;
private var accessChildTimer:Timer;
private var url:String;

private function init():void
{
	accessChildTimer = new Timer( 100, 100 );
	accessChildTimer.addEventListener( TimerEvent.TIMER, onAccessChildTimer );
}

public function loadImage():void
{
	loader = new Loader();
	loader.contentLoaderInfo.addEventListener( Event.COMPLETE, onLoaderComplete );

	var context:LoaderContext = new LoaderContext();
	context.checkPolicyFile = true;

	var request:URLRequest = new URLRequest( url );
	loader.load( request, context );
}

protected function onLoaderComplete( event:Event ):void
{
	var loaderInfo:LoaderInfo = loader.contentLoaderInfo;
	if( loaderInfo.childAllowsParent )
	{
		displayImage();
	}
	else if( loaderInfo.url != this.url )
	{
		var redirectURL:String = loaderInfo.url + "crossdomain.xml"; //Might want to write a method to append a '/' if necessary.
		Security.loadPolicyFile( redirectURL );
		accessChildTimer.reset();
		accessChildTimer.start();
	}
}

protected function onAccessChildTimer( event:TimerEvent ):void
{
	var loaderInfo:LoaderInfo = loader.contentLoaderInfo;
	if( loaderInfo.childAllowsParent )
	{
		accessChildTimer.stop();
		displayImage();
	}
}

private function displayImage():void
{
	//Access BitmapData here
}

Hopefully that helps you out!





2 thoughts on “Updated Solution: AS3 Security Error #2122 with 300 Redirects

  1. FYI,
    The timer will run indefinitely while “childAllowsParent == false”.
    The `function onAccessChildTimer( )` should use a maxCount [for each 100ms iteration] before it “kills” the timer and request; perhaps throw an exception.

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> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>