如何使用 Flex 向 Freshbooks API 进行身份验证?

发布于 2024-08-12 07:46:53 字数 1865 浏览 8 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

权谋诡计 2024-08-19 07:46:53

为了帮助任何希望将来开始使用的人,这里有一个示例:

        // SETUP before any transactions with the web service
        private function setup():void {
            freshCom.setRemoteCredentials(authToken, "x");
        }
        // Fetches a the list of categories from the FreshBooks server
        private function getCategories():void {
            var categoryRequest:XML = <request method='category.list' />;
            freshCom.send(categoryRequest);
        }
        private function handleResult(event:ResultEvent):void {
            // insert breakpoint here to view result
        }   
    ]]>
</mx:Script>
<mx:HTTPService id="freshCom"  url="{serviceURL}" result="handleResult(event)" method="POST" contentType="application/xml" />

To help anyone looking to get started in future here's an example:

        // SETUP before any transactions with the web service
        private function setup():void {
            freshCom.setRemoteCredentials(authToken, "x");
        }
        // Fetches a the list of categories from the FreshBooks server
        private function getCategories():void {
            var categoryRequest:XML = <request method='category.list' />;
            freshCom.send(categoryRequest);
        }
        private function handleResult(event:ResultEvent):void {
            // insert breakpoint here to view result
        }   
    ]]>
</mx:Script>
<mx:HTTPService id="freshCom"  url="{serviceURL}" result="handleResult(event)" method="POST" contentType="application/xml" />

不顾 2024-08-19 07:46:53

如果我记得的话,如果提供的凭据失败,您将看到浏览器身份验证窗口。

来自 FreshBook API 文档:

为您启用 API 访问权限后
帐户,您将获得一个独特的
身份验证令牌。对于每个 API
您提出的请求,您需要
使用基本 HTTP 呈现此令牌
身份验证。

您使用的身份验证令牌应与您的用户名不同。是吗?

If I recall, you'll get the browser authentication window if the credentials provided fail.

From the FreshBook API docs:

After enabling API access for your
account, you'll be given a unique
authentication token. For every API
request you make, you'll need to
present this token using basic HTTP
authentication.

The auth token you use should be different from your username. Is it?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文