For an internal project I created an Adobe AIR application that connects to the Basecamp API. We have been using the application since the launch of Adobe AIR. Back then I had a problem connecting to the API using the HTTPService, because the combination of Basic Authentication and the https protocol was not supported. After several unsuccessful experiments I decided to go for an approach that uses a PHP Proxy on the server that delegates the calls to the Basecamp API. However the downside of this approach is that this setup needs extra configuration and a server-side deployment.

This week I got the chance to pick it up again and I stumbled upon this forum post on the Basecamp Forum. I found this piece of code posted on Verveguy’s blog. And it actually did the trick.

private function addAuthHeader(service:HTTPService,
                     username:String, password:String):void
{
    //add the header to request
    var enc:Base64Encoder = new Base64Encoder();
    enc.encode(username + ":" + password);
    service.headers["Authorization"] = "Basic " + enc.toString();
}

For everybody who needs to connect a Flex application to an API that uses https in combination with Basic Authentication, here is the solution. All credits to the original blog post.

Tagged with:
 

7 Responses to Flex HTTPService and Basic Authentication

  1. [...] application is not that trivial as it would sound. A while ago I wrote a blog post about this (read it here), but this approach only works when you run it from an AIR application. The same code doesn’t [...]

  2. [...] But again like the Twitter API, the Basecamp API uses Basic Authentication (see my previous post: Flex HTTPService and Basic Authentication) and also in the case of Silverlight, it appeared to be a [...]

  3. @hufkens Running into basic auth problem on flex app. Did you commit changes http://bit.ly/w7pJu to your svn repo http://bit.ly/cQC3SF ?thnx

  4. Has Flex SDK 4.1 broken this in your project? I was using same technique and have run into problems with 4.1.

  5. Alain says:

    Could be, I have not tested it with Flex 4.1 yet.

  6. Jack says:

    Thanks, you just saved me. Worked fine in Flex 4.5.1 in a mobile app.

  7. Vishal says:

    private function authAndSend(service:HTTPService):void
    {
    var encoder:Base64Encoder = new Base64Encoder();
    //encoder.insertNewLines = false; // see below for why you need to do this
    encoder.encode(“test99:198$elect”);
    service.headers["Authorization"] = “Basic ” + encoder.toString();
    service.send();
    }

    I am not able to get it work in Flex Mobile 4.5

    Jack can you help me

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Set your Twitter account name in your settings to use the TwitterBar Section.