Katsupoy
Katsupoy

Reputation: 75

How to authenticate to Freshbooks API using Flex?

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.

Upvotes: 0

Views: 803

Answers (2)

Chris Beech
Chris Beech

Reputation: 1

To help anyone looking to get started in future here's an example:

        // SETUP before any transactions with the web service
        private function setup():void {
            freshCom.setRemoteCredentials(authToken, "x");
        }
        // Fetches a the list of categories from the FreshBooks server
        private function getCategories():void {
            var categoryRequest:XML = <request method='category.list' />;
            freshCom.send(categoryRequest);
        }
        private function handleResult(event:ResultEvent):void {
            // insert breakpoint here to view result
        }   
    ]]>
</mx:Script>
<mx:HTTPService id="freshCom"  url="{serviceURL}" result="handleResult(event)" method="POST" contentType="application/xml" />

Upvotes: 1

rogueg
rogueg

Reputation: 303

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?

Upvotes: 0

Related Questions