views:

140

answers:

2

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.

A: 

If I recall, you'll get the browser authentication window if the credentials provided fail.

From the FreshBook API docs:

After enabling API access for your account, you'll be given a unique authentication token. For every API request you make, you'll need to present this token using basic HTTP authentication.

The auth token you use should be different from your username. Is it?

rogueg
Yes, I'm using an auth token different from my username. But it is being entered in the 'username' field for the HTTP authentication. For example, if my auth token is: "201e1e7f485ao1c2e2cac991defa4594"Here's what I put in encoder.encode:encoder.encode("201e1e7f485ao1c2e2cac991defa4594:X"); But it doesn't work. However, if I input the same auth token in the 'username' field in the browser prompt, I am able to authenticate to freshbooks successfully.
Katsupoy
A: 
Chris Beech
Annoying it wont display all my code.
Chris Beech