【问题标题】:How to authenticate to Freshbooks API using Flex?如何使用 Flex 对 Freshbooks API 进行身份验证?
【发布时间】:2009-11-23 15:29:30
【问题描述】:

我是 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) 中。

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

谢谢。

【问题讨论】:

    标签: apache-flex httpservice freshbooks-api


    【解决方案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" />
    

    【讨论】:

      【解决方案2】:

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

      来自 FreshBook API 文档:

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

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

      【讨论】:

      • 是的,我使用的身份验证令牌与我的用户名不同。但它被输入到 HTTP 身份验证的“用户名”字段中。例如,如果我的身份验证令牌是:“201e1e7f485ao1c2e2cac991defa4594”这是我在encoder.encode中输入的内容:encoder.encode("201e1e7f485ao1c2e2cac991defa4594:X");但它不起作用。但是,如果我在浏览器提示符的“用户名”字段中输入相同的身份验证令牌,我就能够成功地对 Freshbooks 进行身份验证。
      猜你喜欢
      • 2011-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-23
      • 1970-01-01
      • 2018-11-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多