2010-04-30 113 views
2

我试图从一些CURL代码转换为FLEX/ActionScript。 因为我对CURL百分之百无知,50%对于Flex一无所知,90%对HTTP一无所知... 我有一些重大的困难。将CURL转换为FLEX HTTPRequests

,下列curl代码为http://code.google.com/p/ga-api-http-samples/source/browse/trunk/src/v2/accountFeed.sh

我有充分的理由相信,它的正常工作。

 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" 

以下是我失败的企图上述卷曲转化为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); 

这可能为您提供各种具有良好的笑,那也没关系,但如果你能找到任何可怜我,请让我知道我错过了什么。我很乐意承认功能上的不足,因此让我知道我是多么愚蠢是可选的。

回答

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 = "[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); 
} 

所以首先,您获得经过身份验证的令牌(然后您将其保存并重新用于所有的URLRequest标头),然后您可以拨打Google Analytics(分析)。

希望帮助, 兰斯

2

如果你打算做一个很大的发展通过HTTP,你也应该考虑使用类似Charles ProxyFirebug调试并查看您的实际HTTP请求。