Archive for May, 2009

How to connect to Google Analytics Data API in AIR

I had a bit of trouble doing just this a while ago, ended up trying many different ways and finally a reader of this blog, Nakamachi, gave me the solution. Because I think many of you are trying to connect to Google Analytics Data API ( or any Google APIs for that matters) I will give you here my solution. My solution only works in AIR; I don’t know why, but as soon as I put it on the web, it stops working. I have found a solution for that problem too but I will keep it for another post.
Here is how you can connect to GData:

import org.httpclient.HttpClient;
import com.adobe.net.URI;
import org.httpclient.events.HttpResponseEvent;
import org.httpclient.events.HttpRequestEvent;
import org.httpclient.HttpRequest;
import org.httpclient.http.Get;
import org.httpclient.events.HttpDataEvent;
 
var _firstBuffer:String = "";
var _secondBuffer:String = "";
 
//this first connection will get the authorization token by sending the user name and password for Google Analytics
function submitForm(email:String, password:String):void {
  var client:HttpClient = new HttpClient();
  var uri:URI = new URI("https://www.google.com/accounts/ClientLogin");
  var variables:Array = [{name:"accountType", value:"GOOGLE"}, {name:"Email", value: email}, {name:"Passwd", value: password}, {name:"service", value: "analytics"}, {name:"source", value: "your-application-identifier"}];
  client.listener.onData = _onFirstData;
  client.listener.onComplete = _onLoaderComplete;
  client.postFormData(uri, variables);
}
 
function _onFirstData(event:HttpDataEvent):void {
  _firstBuffer += event.readUTFBytes();
}
 
//this second connection will request information to GData
//in this case account info for Google Analytics by
//putting the auth token in the header of the request
function _onLoaderComplete(event:HttpResponseEvent):void {
  var tempArray:Array = _firstBuffer.split("Auth=");
 var  _authToken:String = String(tempArray[1]);
 
  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 {
    _secondBuffer += event.readUTFBytes();
  }
  client.listener.onComplete = _onSecondLoaderComplete;
  client.request(uri, request);
}
 
function _onSecondLoaderComplete(event:HttpResponseEvent):void {
  trace (_secondBuffer);
}

As you can see in order to circumvent the Authorization header restriction I am using the as3httpclientlib library which requires the as3corelib and as3crypto libraries to compile. Using the same principle you should be able to connect to Google APIs from your AIR application.

, , , , , , , ,

14 Comments


My take on decompiling SWF

I didn’t read (or listen, I don’t remember if it was a video) Lee Brimelow article on decompilation but I know he his not against it. In the same way, I also believe decompiling other’s work is a positive thing. I remember doing my first webpage (html) and to add cool stuff to it (nice javascript and strange html tags) all you had to do was to right click, choose view source and apply it in your context. Back then you were on your own, but what you would learn there you wouldn’t forget because you worked to extract the knowledge from the cryptic code. What you learned was then added to your toolbox; you could build better webpages. The entire internet grew from the view source feature.

I believe it should be the same with Flash. Every day you can see an awesome piece of creativity if you go to theFWA.com; sometimes you know how it is done, sometimes you don’t. This is the time where you should reverse engineer the work. Learn how they did it, how they solved certain problems. You should do so, not to replicate the piece, but to make it your own. Mix it with what you already know or give it a new twist; do something even better. That is how you should pay back.

The community, platform (Flash) would grow from this. People would start decompiling your work and even better pieces would be created. In the end, we would all win from this. Better creations means more confidence in the platform, more money invested into it (Adobe, web agencies) and more jobs.

If you think individually, you’ll only see the bad sides of decompiling, but if you open your mind and have a broader view, you’ll understand the benefits.

, , , ,

6 Comments


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.

, , , , , , , , ,

12 Comments