如何使用 Flex 向 Freshbooks API 进行身份验证?
我是 Flex (FLex Builder 3.0) 的新手,我正在创建一个简单的应用程序,它基本上只是通过 HTTPS 对 Freshbooks API (www.freshbooks.com) 进行身份验证。无需发送任何其他内容。
到目前为止,我遇到的情况是这样的:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
import mx.utils.Base64Encoder;
private function authAndSend(service:HTTPService):void
{
var encoder:Base64Encoder = new Base64Encoder();
encoder.insertNewLines = false;
encoder.encode("<freshbooks auth token>:X");
//for some reason, freshbooks says that the authentication token
//is the username, and the password could just be a dummy value.
service.headers = {Authorization:"Basic " + encoder.toString()};
service.send();
}
private function freshBooksHandler(event:ResultEvent):void{
txtArea1.text = event.result.toString();
}
]]>
</mx:Script>
<mx:HTTPService id="freshBooksService" url="https://myaccount.freshbooks.com/api/2.1/xml-in"
result="freshBooksHandler(event)" resultFormat="xml">
</mx:HTTPService>
<mx:Button x="59" y="48" label="Button" click="authAndSend(freshBooksService)"/>
<mx:TextArea x="59" y="78" width="401" height="242" id="txtArea1"/>
</mx:Application>
问题是,当我单击按钮时,浏览器会弹出一个窗口,要求我输入 FreshBooks 服务的用户名和密码。
我如何对其进行编码,以便将用户名(freshbooks 给出的身份验证令牌)从 Flex 本身而不是浏览器发送到服务器?我的预期结果是浏览器不会弹出窗口,并且 freshbooks 服务器返回的任何内容都将显示在文本区域(txtArea1)中。
注意:如果我在浏览器弹出窗口中输入身份验证令牌,应用程序能够正确地将输出显示到文本区域。
谢谢。
I am new to Flex (FLex Builder 3.0), and I'm creating a simple application that would basically just authenticate to the Freshbooks API (www.freshbooks.com) via HTTPS. without sending anything else.
Here's what I have so far:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
import mx.utils.Base64Encoder;
private function authAndSend(service:HTTPService):void
{
var encoder:Base64Encoder = new Base64Encoder();
encoder.insertNewLines = false;
encoder.encode("<freshbooks auth token>:X");
//for some reason, freshbooks says that the authentication token
//is the username, and the password could just be a dummy value.
service.headers = {Authorization:"Basic " + encoder.toString()};
service.send();
}
private function freshBooksHandler(event:ResultEvent):void{
txtArea1.text = event.result.toString();
}
]]>
</mx:Script>
<mx:HTTPService id="freshBooksService" url="https://myaccount.freshbooks.com/api/2.1/xml-in"
result="freshBooksHandler(event)" resultFormat="xml">
</mx:HTTPService>
<mx:Button x="59" y="48" label="Button" click="authAndSend(freshBooksService)"/>
<mx:TextArea x="59" y="78" width="401" height="242" id="txtArea1"/>
</mx:Application>
The problem is then when I click the button, the browser brings up a pop-up asking for my username and password to the FreshBooks service.
How do I code it such that the username (the authentication token given by freshbooks) would be sent to the server from within Flex itself and not the browser? My expected outcome is that there would be no pop-ups from the browser and whatever the freshbooks server returned would be displayed in the text area (txtArea1).
Note: If I input the authentication token to the browser pop-up, the application is able to correctly display the output to the text area.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了帮助任何希望将来开始使用的人,这里有一个示例:
To help anyone looking to get started in future here's an example:
如果我记得的话,如果提供的凭据失败,您将看到浏览器身份验证窗口。
来自 FreshBook API 文档:
您使用的身份验证令牌应与您的用户名不同。是吗?
If I recall, you'll get the browser authentication window if the credentials provided fail.
From the FreshBook API docs:
The auth token you use should be different from your username. Is it?