将 CURL 转换为 FLEX HTTPRequest
我正在尝试将一些 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="[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"
下面是我将上面的 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 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);
这可能会让你们大家开怀大笑,这没关系,但如果你们能对我有任何怜悯,请让我知道我错过了什么。我很容易承认功能上的无能,因此让我知道自己有多么愚蠢是可选的。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嘿,有趣的问题。
curl 是一个 unix 命令,您可以在终端中运行它。它返回您请求的 url 的原始 html 页面。
因此,您不能只将curl 命令复制到Actionscript 中,因为Flash/Flex 不允许您执行命令行脚本(AIR 2.0 允许,但这与此处无关)。
curl 命令的目标是从 Google 获取身份验证令牌。 ,您需要做的就是将您的
GoogleAuth
变量设置为使用您提供的参数向 google 发出的第一个 HTTP 请求的结果,如下所示(伪代码,尚未测试):因此 首先,您获取经过身份验证的令牌(然后将其保存并在所有 URLRequest 标头中重复使用),然后调用 Google Analytics。
希望有帮助,
槊
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):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
如果您要通过 HTTP 进行大量开发,您还应该考虑使用 Charles Proxy 或 Firebug 来调试和查看实际的 HTTP 请求。
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.