views:

801

answers:

5

This is related to this question. I'm writing a Flex app (a WindowedApplication) that uses REST. Everything's fine when I post with valid authentication, but if I happen to pass an invalid username or password to the REST API (a Twitter REST API, to be specific), an authentication dialog pops up.

That's not a desirable user experience, and it happens both when I use HTTPService and URLRequest. There doesn't seem to be an event I can catch to cancel the dialog.

Here's what my code looks like:

    var request:URLRequest = new URLRequest('http://twitter.com/statuses/update.json');
 request.method = URLRequestMethod.POST;
 var encoder : Base64Encoder = new Base64Encoder();
    encoder.encode(this.user + ':' + this.password);
    request.requestHeaders.push(new URLRequestHeader("Authorization", "Basic " + encoder.toString()));
    var params:Object = new Object();
    params.status = msg;       
    request.data = params;

 var loader:URLLoader = new URLLoader();
    loader.addEventListener(Event.COMPLETE, HandleRequestComplete);
 loader.load(request);

Am I missing something? Is there a better way to approach this?

+1  A: 

From the Twitter API Doc here:

suppress_response_codes: If this parameter is present, all responses will be returned with a 200 OK status code - even errors. This parameter exists to accommodate Flash and JavaScript applications running in browsers that intercept all non-200 responses. If used, it's then the job of the client to determine error states by parsing the response body. Use with caution, as those error messages may change.

Brandon
Oh, awesome! Thanks!
Jim
A: 

This workaround only works with the Twitter API, what about any HTTP POST that fails authentication? How can the pop-up dialog be suppressed?

A: 

I don't know if this works in a normal Flex app, but in AIR applications you can set a list of allowed response codes to be considered valid.

Marc Hughes
+1  A: 

That is becouse your URLRequest is handling authentication. To avoid that do the next:

request.authenticate = false;

Regards!

Alain.

A: 

Any thoughts on how to prevent the dialog when using HTTPService?

DmcQ
I'm looking for a RESTConnector (probably build off of Socket) that re-implements the HTTP protocol just so it does HTTP authentication as well as DELETE and PUT.Any suggestions are welcome
Richard Haven