将CURL转换为FLEX HTTPRequests

Jos*_*hua 2 apache-flex https curl http actionscript-3

我试图从一些CURL代码转换为FLEX/ActionScript.由于我对CURL 100%无知,而且对于Flex有50%的无知,而对HTTP一般有90%的无知......我遇到了一些重大困难.

以下CURL代码来自http://code.google.com/p/ga-api-http-samples/source/browse/trunk/src/v2/accountFeed.sh

我完全有理由相信它运作正常.

       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"
Run Code Online (Sandbox Code Playgroud)

以下是我将上述CURL转换为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);
Run Code Online (Sandbox Code Playgroud)

这可能会让你们大笑起来,这没关系,但如果你能对我发现任何遗憾,请告诉我我所缺少的东西.我很容易承认功能上的无能,因此让我知道我是多么愚蠢是可选的.

Lan*_*ard 5

嘿,有趣的问题.

curl是一个unix命令,你可以在终端中运行它.它返回您请求的URL的原始html页面.

因此,您不能将curl命令复制到Actionscript中,因为Flash/Flex不允许您执行命令行脚本(AIR 2.0会这样做,但这与此无关).

curl命令的目标是从Google获取身份验证令牌.因此,您需要做的就是将您的GoogleAuth变量设置为第一个HTTP请求的结果,使用您提供的参数进行谷歌搜索,如下所示(伪代码,尚未测试):

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);
}
Run Code Online (Sandbox Code Playgroud)

首先,您获得经过身份验证的令牌(然后您可以在所有URLRequest标头中保存并重复使用),然后拨打Google Analytics.

希望有所帮助,兰斯