Posts Tagged as3httpclientlib
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.


