HTTP Authentication with Flash
For posterity and the rest of the internet, here is the trick to get flash to load variables using LoadVars on a password protected site, when it's using http authentication. I have found parts of examples, but not an actual working example, so here it is.
First of all, get the actionscript Base64 encoder class.
Put it in your classpath or the same directory as your .fla file.
Then do something like this:
var loadvars = new LoadVars();
var str = Base64.Encode("username:password");
loadvars.addRequestHeader("Authorization:", "Basic "+str);
loadvars.load("some_password_protected_url.php");
That's all.
Y'know, that's the whole reason I ported that code – I was playing around with the del.ico.us api. Should've put up an example, but I haven't had time to get back into it.
Sometime soon, hopefully. In the meantime, thanks for posting this…
You're welcome.
Thanks for porting the base64 class :)
Base 64 works well but, when i test it with delicious api i could not get satisfied.
Descussion on livedocs ends with "you cannot access headers directly from Flash" discussion. Is there any way to reach delicious api without asp of php gateway which sends headers for flash and returns results back? Thanks.
Hi Inan,
you're only able to modify the header via addRequestHeader when using the post method. You could use the sendAndLoad method to achieve that.
Cheers, Kai.
Here You will find ASCrypt extension for Flash. It is very handy.
Regards
Marcin Andrzejewski
Sometime soon, hopefully. In the meantime, thanks for posting this.. one day…
This solution does not work anymore in IE 7. :-(
Has someone fixed this in IE7?
hi
I am using flash and actionscript client to access my web service which require authentication. I am using the above technique it works fine in firefox but no headers are sent in case of IE…Can anyone help me for this..I need it quick..
thanks
Hi Mukul,
its more than two years ago, but Ive one question about what you wrote:
I am using flash and actionscript client to access my web service which require authentication. I am using the above technique it works fine in firefox…
How exactly you create the access to a webservice which requires authentication?
Thats my standard-AS2 to call a webservice:
var my_wsc:WebServiceConnector = new WebServiceConnector();
my_wsc.WSDLURL = http://xyz/webservice.asmx?WSDL;
my_wsc.addEventListener(result, my_listener);
my_wsc.operation = WS-Operation;
my_wsc.params = [...];
my_wsc.trigger();
function my_listener(obj:Object) {
var res:Object = obj.target.results;
}