Translating CURL to FLEX HTTPRequests

I am trying to convert from some CURL code to FLEX / ActionScript. Since I am 100% unaware of CURL and 50% unaware of Flex and 90% unaware of HTTP at all ... I have some significant difficulties.

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 is working correctly.

       USER_EMAIL="myaccount@gmail.com" #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"

      

Below is my failed 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=myaccount@gmail.com " + 
        "-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);

      

It probably makes you all laugh, which is good, but if you can find any pity for me, please let me know what I'm missing. I readily admit functional ineptitude, so letting me know how stupid I am is optional.

+2


a source to share


2 answers


Hey, interesting question.

curl is a unix command that you run in a terminal. It returns the raw html page of the url you requested.

So you can't just copy the curl command into ActionScript, since Flash / Flex doesn't allow command line scripting (AIR 2.0 does, but that doesn't matter here).

The purpose of the curl command is to get an authentication token from Google. So, all you have to do is set your variable GoogleAuth

as a result of the first HTTP request to google with the parameters you provide, something like this (pseudocode, not tested):



var authenticate:URLRequest = new URLRequest("https://www.google.com/accounts/ClientLogin")
var variables:URLVariables = new URLVariables();
variables.Email = "myemail@gmail.com";
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 an authenticated token (which you then save and reuse in all of your URLRequest headers), then you make your call to Google Analytics.

Hope this helps, Spear

+5


a source


If you're going to be doing a lot of HTTP work, you should also learn something like Charles Proxy or Firebug to debug and see your actual HTTP requests.



+2


a source







All Articles