Dealing with AS3’s ArgumentError: Error #2096 while pushing an innocuous URLRequestHeader

I’m working on a Flex mobile application that has me parsing SIDs and Auth values from Google’s client login. You may find yourself in this situation if you need to make requests to Google’s various APIs. In my case, I’m using Google Reader, and it requires that I pass in an Authorization header with all of my URLRequests, the value of which needs to be taken from Google’s client login response. This seemed like an easy task, but I kept hitting an annoying error, which turned out to be a red herring:

ArgumentError: Error #2096: The HTTP request header GoogleLogin auth=(long string)
cannot be set via ActionScript.

This error strongly indicates that I was doing something that transgresses AIR’s security sandbox, which is simply not the case.

It turns out that I had grabbed the value of auth from Google’s response, but I had failed to notice that Google was putting a newline character on the end of the auth string. When I fixed this by stripping the newline and putting the modified String into the URLRequestHeader, the runtime had no problem with it.

Thus, if you have an error that sounds like what I’ve described, make sure you’re not trying to set the URLRequestHeader with a value that has a newline or similar character somewhere in it. You can strip it out any number of ways, including String‘s replace() function.