I am new to Flex (FLex Builder 3.0), and I'm creating a simple application that would basically just authenticate to the Freshbooks API (www.freshbooks.com) via HTTPS. without sending anything else.
Here's what I have so far:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
import mx.utils.Base64Encoder;
private function authAndSend(service:HTTPService):void
{
var encoder:Base64Encoder = new Base64Encoder();
encoder.insertNewLines = false;
encoder.encode("<freshbooks auth token>:X");
//for some reason, freshbooks says that the authentication token
//is the username, and the password could just be a dummy value.
service.headers = {Authorization:"Basic " + encoder.toString()};
service.send();
}
private function freshBooksHandler(event:ResultEvent):void{
txtArea1.text = event.result.toString();
}
]]>
</mx:Script>
<mx:HTTPService id="freshBooksService" url="https://myaccount.freshbooks.com/api/2.1/xml-in"
result="freshBooksHandler(event)" resultFormat="xml">
</mx:HTTPService>
<mx:Button x="59" y="48" label="Button" click="authAndSend(freshBooksService)"/>
<mx:TextArea x="59" y="78" width="401" height="242" id="txtArea1"/>
</mx:Application>
The problem is then when I click the button, the browser brings up a pop-up asking for my username and password to the FreshBooks service.
How do I code it such that the username (the authentication token given by freshbooks) would be sent to the server from within Flex itself and not the browser? My expected outcome is that there would be no pop-ups from the browser and whatever the freshbooks server returned would be displayed in the text area (txtArea1).
Note: If I input the authentication token to the browser pop-up, the application is able to correctly display the output to the text area.
Thanks.