views:

144

answers:

2

I am trying to convert from some CURL code to FLEX/ActionScript. Since I am 100% ignorant about CURL and 50% ignorant about Flex and 90% ignorant on HTTP in general... I'm having some significant difficulty.

The following CURL code is from http://code.google.com/p/ga-api-http-samples/source/browse/trunk/src/v2/accountFeed.sh

I have every reason to believe that it's working correctly.

       USER_EMAIL="[email protected]" #Insert your Google Account email here
       USER_PASS="secretpass" #Insert your password here

       googleAuth="$(curl https://www.google.com/accounts/ClientLogin -s \
       -d Email=$USER_EMAIL \
       -d Passwd=$USER_PASS \
       -d accountType=GOOGLE \
       -d source=curl-accountFeed-v2 \
       -d service=analytics \
     | awk /Auth=.*/)"
       feedUri="https://www.google.com/analytics/feeds/accounts/default\
       ?prettyprint=true"

       curl $feedUri --silent \
       --header "Authorization: GoogleLogin $googleAuth" \
       --header "GData-Version: 2"

The following is my abortive attempt to translate the above CURL to AS3

    var request:URLRequest=new URLRequest("https://www.google.com/analytics/feeds/accounts/default");
    request.method=URLRequestMethod.POST;
    var GoogleAuth:String="$(curl https://www.google.com/accounts/ClientLogin -s " + 
        "-d [email protected] " + 
        "-d Passwd=secretpass " + 
        "-d accountType=GOOGLE " + 
        "-d source=curl-accountFeed-v2" + 
        "-d service=analytics " + 
        "| awk /Auth=.*/)";
    request.requestHeaders.push(new URLRequestHeader("Authorization", "GoogleLogin " + GoogleAuth));
    request.requestHeaders.push(new URLRequestHeader("GData-Version", "2"));
    var loader:URLLoader=new URLLoader();
    loader.dataFormat=URLLoaderDataFormat.BINARY;
    loader.addEventListener(Event.COMPLETE, GACompleteHandler);
    loader.addEventListener(IOErrorEvent.IO_ERROR, GAErrorHandler);
    loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, GAErrorHandler);
    loader.load(request);

This probably provides you all with a good laugh, and that's okay, but if you can find any pity on me, please let me know what I'm missing. I readily admit functional ineptitude, therefore letting me know how stupid I am is optional.

+2  A: 

Hey, interesting question.

curl is a unix command, which you'd run in the terminal. It returns the raw html page of the url you've requested.

So you can't just copy the curl command into Actionscript since Flash/Flex doesn't allow you to execute command line scripts (AIR 2.0 does, but that's not relevant here).

The goal of the curl command is to get the Authentication token from Google. So all you need to do is set your GoogleAuth variable to the result of a first HTTP Request to google with the parameters you're providing, something like this (pseudo code, haven't tested):

var authenticate:URLRequest = new URLRequest("https://www.google.com/accounts/ClientLogin")
var variables:URLVariables = new URLVariables();
variables.Email = "[email protected]";
variables.Passwd = "mypass";
variables.accountType = "GOOGLE";
variables.source = "MyApplication Name";
variables.service = "analytics";
authenticate.data = variables;
var loader:URLRequest = new URLRequest();
loader.addEventListener(Event.COMPLETE, authenticated);
loader.load(authenticate);

protected function authenticated(event:Event):void
{
  var request:URLRequest=new URLRequest("https://www.google.com/analytics/feeds/accounts/default");
  request.method = URLRequestMethod.POST;
  var GoogleAuth:String = event.data;
  request.requestHeaders.push(new URLRequestHeader("Authorization", "GoogleLogin " + GoogleAuth));
  request.requestHeaders.push(new URLRequestHeader("GData-Version", "2"));
  var loader:URLLoader = new URLLoader();
  loader.dataFormat = URLLoaderDataFormat.BINARY;
  loader.addEventListener(Event.COMPLETE, GACompleteHandler);
  loader.addEventListener(IOErrorEvent.IO_ERROR, GAErrorHandler);
  loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, GAErrorHandler);
  loader.load(request);
}

So first, you get the authenticated token (which you then save and reuse in all your URLRequest headers), then you make your call to Google Analytics.

Hope that helps, Lance

viatropos
A: 

If you're going to be doing a lot of development over HTTP, you should also look into using something like Charles Proxy or Firebug to debug and view your actual HTTP requests.

merlinc