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 本身而不是浏览器发送到服务器?我的预期结果是浏览器不会弹出任何窗口,并且无论新鲜书服务器返回什么,都将显示在文本区域 (txtArea1) 中。

注意:如果我将身份验证令牌输入到浏览器弹出窗口,应用程序能够正确地将输出显示到文本区域。

谢谢。

4

2 回答 2

1

为了帮助任何希望在未来开始的人,这里有一个例子:

        // 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" />

于 2010-02-09T14:41:28.680 回答
0

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

来自 FreshBook API 文档:

为您的帐户启用 API 访问后,您将获得一个唯一的身份验证令牌。对于您发出的每个 API 请求,您都需要使用基本 HTTP 身份验证来提供此令牌。

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

于 2009-11-23T16:50:43.593 回答