Problem:
Your AIR application needs to validate the username / password and handle failure gracefully within the application. If the user enters the wrong information you do not want them to be prompted with a windows prompt asking them to enter their Twitter username / password. If the application needs to hold on to the credentials for any reason the windows authentication to the API will break the AIR application.

"The server twitter.com at Twitter API requires a username and password."

Windows Authentication Pop-up Message.

Windows Authentication Pop up message.

Solution:
The URLRequest object has a property called 'authenticate' that must be set to false.

Actionscript:
  1. /**
  2. *
  3. * Setting result.authenticate to false prevents the operating system from
  4. * taking over and prompting the user to authenticate.  It allows the AIR
  5. * application to take the correct action.
  6. *
  7. */  
  8. private function twitterRequest (url : String):URLRequest
  9. {
  10.     var result:URLRequest = new URLRequest (url);
  11.     if (this.authorizationHeader){
  12.         result.authenticate = false// <--------- Most Important Line of Code!!!!
  13.         result.requestHeaders = [this.authorizationHeader]
  14.     }
  15.     return result;
  16. }

Most of the code from the example attached below is from the Google code repository:

http://code.google.com/p/twitterscript/

Here are the source files to an example AIR application that uses actionscript to verify Twitter credentials without prompting a windows box on failure:

Source Files

I would like to give credit to Clayton (file_cabinet) for coming up with the solution to this problem. Thanks Clayton!

Tags: , , , , , ,