views:

25

answers:

0

As the google trends api has still not yet been published I wanted to try and get the data using AIR. I am using the as3httpclient lib to login to my google account, here is how I'm doing so far:

Credit for the approach to:

this blog post

//this first connection will get the authorization token by sending the user name and password for Google Analytics
        protected 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);
        }

        protected 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
        protected 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("http://www.google.com/trends/viz?q=squash&graph=all_csv&sa=N");
            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);
        }

        protected function _onSecondLoaderComplete(event:HttpResponseEvent):void {
            trace (_secondBuffer);
        }

Ok when this runs the entire source of the page is returned. I want to access the CSV data from this page, if you login to your google account and access google trends there is a button to download the data.

I can see the url in the source:

http://www.google.com/trends/viz?q=squash&graph=all_csv&sa=N

This is an example of search for squash in google trends. If i open this link in browser and I am logged in it prompts for a file download.

I need to know roughly how i can get that csv file in air.