Connecting to Google Analytics Data Api using AS3; my failures

EDIT:
Finally I turned my failures into success you can read it here:
How to connect to Google Analytics Data API in AIR
How to connect to Google Analytics Data API in Flash

These past three days I have spent my time trying to connect from an AIR application to Google Analytics Data Api and I have failed. Sometimes you just admit to yourself thatwhat you want to do is just impossible. The problem is that I know it is feasible, Desktop Reporting is doing it with their AIR application called Polaris. Now all I think about is how I can solve this problem. In this post I’m going to show what I tried in order to accomplish this so that it might help others or that others can help me.

The big obstacle at the root of my problem is the fact that you can’t set the Authorization header in ActionScript which is required to make request to the Google server to get data. Here is how I found out about this:

var request:URLRequest = new URLRequest("https://www.google.com/analytics/feeds/accounts/default");
 
request.method = URLRequestMethod.GET;
var tempHeader:URLRequestHeader = new URLRequestHeader("Authorization", "GoogleLogin auth=" + _authToken);
request.requestHeaders = [tempHeader];
var  _secondLoader:URLLoader = new URLLoader();
_secondLoader.addEventListener(Event.COMPLETE, _onSecondLoaderComplete, false, 0, true);
_secondLoader.dataFormat = URLLoaderDataFormat.TEXT;
_secondLoader.load(request);

This resulted in the following runtime error:

ArgumentError: Error #2096: The HTTP request header GoogleLogin auth=DQA … -lQ
cannot be set via ActionScript

From that point on I was aware that you couldn’t set the Authorization request header even thought you see misleading information around the web that doesn’t help. My next try was using HTTPService from the Flex library here is what I tried:

gService.url = "https://www.google.com/analytics/feeds/accounts/default";
 
gService.headers["Authorization"] = "GoogleLogin auth=" + _authToken;
var token:AsyncToken = gService.send();
token.addResponder(new Responder(onResponse, onFault));

This ended up with the same result (obviously).

My next try was using the as3httpclient library which I did in this way:

var client:HttpClient = new HttpClient();
var uri:URI = new URI("https://www.google.com/analytics/feeds/accounts/default");
var request:HttpRequest = new Get();
request.addHeader("Authorization", "GoogleLogin auth=" + _authToken);
 
client.listener.onData = function(event:HttpDataEvent):void {
trace (event.readUTFBytes)
};
client.request(uri, request);

This seemed to work a bit better but it always traced “200″ and not the xml feed it was supposed to return.

So I am at this point very frustrated because I am not able to do something I know is feasible. I have other step I can take in my project and I have some not very optimal way of getting the data, but I have a very bitter taste left in my mouth.  If anyone can help me with this, it would be really appreciated.

, , , , , , , , ,

  1. #1 by Nakamachi - May 8th, 2009 at 00:09

    Hi.
    I worried about the same issue URLRequestHeader, too.

    You have an easy mistake in “as3httpclient”.
    “event.readUTFBytes()” is correct.

  2. #2 by zedia.net - May 12th, 2009 at 14:01

    You’re right but I made the mistake while copying the code in the post.

  3. #3 by Nakamachi - May 13th, 2009 at 06:35

    I can get the data from Google Analytics API by using the as3httpclient library.
    However, it saves in the buffer in onData, and after onComplete, it uses it.

    Example:

    var buffer:String = new String();
    client.listener.onData = function(event:HttpDataEvent):void {
    buffer += event.readUTFBytes();
    };
    client.listener.onComplete = function(event:HttpResponseEvent):void {
    // using Google Anaylitics API response data.
    trace(buffer);
    };

  4. #4 by zedia.net - May 13th, 2009 at 10:34

    @Nakamachi
    Thank you for this, I will definitely try it out tonight.

  5. #5 by Railk - May 17th, 2009 at 04:26

    Hi,

    I also wanted to use the google analytics data api and arrived to the same conclusion as you and use as3HttpClient lib.

    Though, it seems authorization header can be used but solely if the crossdomain file policy of the service include this line :

    (cf: http://kb.adobe.com/selfservice/viewContent.do?externalId=kb403184)

    A feature request have been filled at Google ( http://groups.google.com/group/Google-Picasa-Data-API/browse_thread/thread/2f1bfab80ea53c60 ). So there is some hope we can use the authorization header directly with urlrequestheader.

    thx nakamachi for code helps also ^^.

  6. #6 by PB - May 28th, 2009 at 12:09

    You’re right but I made the mistake while copying the code in the post.

  7. #7 by Tracy Spratt - June 14th, 2009 at 19:38

    I also want to access the Google Analytics API from a Flex/AIR.

    Has anyone had any recent success?

  8. #8 by david - December 30th, 2009 at 16:38

    Did anyone solve this problem? I hate to go down the same painful road…. thanks.

  9. #10 by Denis - January 27th, 2011 at 16:54

    Hi,

    actually we build as3 library for google analytics to simplify the process. open source. http://blog.denivip.ru/index.php/2010/12/open-source-actionscript-3-google-analytics-api/

  10. #11 by Leslie T - April 6th, 2011 at 18:36

    @ Denis That’s great, thanks for the share

(will not be published)
Subscribe to comments feed