【问题标题】:Intuit IPP Rest API query string escapeIntuit IPP Rest API 查询字符串转义
【发布时间】:2014-03-19 20:32:07
【问题描述】:

所以,对于Intuit IPP Rest API,

假设我想查询一个名字是 ABC 的客户,我可以使用这样的 http get 请求

https://qb.sbfinance.intuit.com/v3/company/198445012/query?query=select Id from Customer where FullyQualifiedName%3D'ABC'

&3D 是 '=' 的 url 转义,这没有任何问题。

现在如果客户的名字是 A&B,我尝试了这样的查询字符串

select Id from Customer where FullyQualifiedName='A&B'

url编码后是这样的

https://qb.sbfinance.intuit.com/v3/company/198445012/query?query=select Id from Customer where FullyQualifiedName%3D'A%26B'

它会失败。

有什么想法吗?

更新

以上网址是我从 IPP 的 API 浏览器中复制的。

这里是代码,我用的是DevDefined.OAuth

        IConsumerRequest conReq = _oSession.Request();
        conReq = conReq.Get();
        conReq.AcceptsType = "application/xml";
        //conReq = conReq.ForUrl(string.Format(@"https://qb.sbfinance.intuit.com/v3/company/{0}/query?query={1}", Settings.Default.QuickBooksOnlineRealmId, @"select * from Customer where DisplayName='ABC'"));   if use this line, it works fine
        conReq = conReq.ForUrl(string.Format(@"https://qb.sbfinance.intuit.com/v3/company/{0}/query?query={1}", Settings.Default.QuickBooksOnlineRealmId, @"select * from Customer where DisplayName='A&B'"));
        try
        {                
            string str = conReq.ReadBody();

        catch (Exception ex)
        {
            //ex.Message
        }

这样返回的xml数据

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<IntuitResponse xmlns="http://schema.intuit.com/finance/v3" time="2014-03-20T06:24:12.408-07:00">
  <Fault type="ValidationFault">
    <Error code="4000">
      <Message>Error parsing query</Message>
      <Detail>QueryParserError: Invalid content. Lexical error at line 1, column 44.  Encountered: &lt;EOF&gt; after : &quot;\'A&quot;</Detail>
    </Error>
  </Fault>
</IntuitResponse>

我不是 100% 确定,昨天当我测试时,它实际上返回了一些说 oauth 失败的东西。但这就是我今天早上得到的。

其实,你可以在 IPP 的 API explorer 中试一试,结果是一样的。

ForUrl 的 devdefined 代码

public static IConsumerRequest ForUrl(this IConsumerRequest request, string url)
{
    request.Context.RawUri = new Uri(url);
    return request;
}

这会将网址编码为

https://qb.sbfinance.intuit.com/v3/company/1122502005/query?query=select%20*%20from%20Customer%20where%20DisplayName='A&B'

好的,终于,找到问题了:

真正的问题是 Uri(url) 不会转义 'A&B' 中的 & 因为它不知道它是 url & 还是数据的一部分,所以我更改了以下行

conReq = conReq.ForUrl(string.Format(@"https://qb.sbfinance.intuit.com/v3/company/{0}/query?query={1}", Settings.Default.QuickBooksOnlineRealmId, @"select * from Customer where DisplayName='A&B'"));

作为

conReq = conReq.ForUrl(string.Format(@"https://qb.sbfinance.intuit.com/v3/company/{0}/query?query={1}", Settings.Default.QuickBooksOnlineRealmId, Uri.EscapeDataString(@"select * from Customer where DisplayName='A&B'")));

首先使用 Uri.EscapeDataString 转义数据查询字符串。

【问题讨论】:

    标签: api rest intuit-partner-platform intuit


    【解决方案1】:

    如果您创建一个名为“A&B”的客户,则 V3 服务会返回该客户对象,如下所示 -

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <IntuitResponse xmlns="http://schema.intuit.com/finance/v3" time="2014-03-20T01:54:46.834-07:00">
        <Customer domain="QBO" sparse="false">
            <Id>10</Id>
            <SyncToken>0</SyncToken>
            <MetaData>
                <CreateTime>2014-03-20T01:54:46-07:00</CreateTime>
                <LastUpdatedTime>2014-03-20T01:54:47-07:00</LastUpdatedTime>
            </MetaData>
            <FullyQualifiedName>A&amp;B</FullyQualifiedName>
            <DisplayName>A&amp;B</DisplayName>
            <PrintOnCheckName>A&amp;B</PrintOnCheckName>
            <Active>true</Active>
            <Taxable>true</Taxable>
            <Job>false</Job>
            <BillWithParent>false</BillWithParent>
            <Balance>100.00</Balance>
            <BalanceWithJobs>100.00</BalanceWithJobs>
            <PreferredDeliveryMethod>Email</PreferredDeliveryMethod>
        </Customer>
    </IntuitResponse>
    

    要按名称检索此对象,您需要使用以下查询

    SELECT * FROM Customer WHERE DisplayName = 'A&B'
    

    但是需要像下面这样进行url编码

    SELECT+*+FROM+Customer+WHERE+DisplayName+%3D+%27A%26B%27
    

    实现这一点的 Java 代码 -

    Customer customer = GenerateQuery.createQueryEntity(Customer.class);
    String query = select($(customer)).where($(customer.getDisplayName()).eq("A&B")).generate();
    // Query output - SELECT * FROM Customer WHERE DisplayName = 'A&B'
    String encodedUrl = URLEncoder.encode(query, "UTF-8");
    

    完美运行。 (Devkit 可以很好地处理所有这些)

    希望这能回答你的问题。

    谢谢

    【讨论】:

    • 我用我的实际代码更新了这个问题,我认为 devdefined 处理了 url 编码。从返回的消息中,似乎是 & 导致了问题,但仍然不知道如何解决它
    • 我不确定它在 dev-defined 中是如何工作的(很可能,您需要对其进行显式编码)。在 value 子句中,您通过了 - %3D'A%26B'。您可以看到单引号未编码。请像我在使用 java 尝试此查询时所做的那样注意这一点。您可以使用 IPP 的 .net lib 进行此调用。 developer.intuit.com/docs/0025_quickbooksapi/0055_devkits/…。 Devkit 下载链接 - developer.intuit.com/docs/0025_quickbooksapi/0055_devkits 谢谢
    【解决方案2】:

    请在此处查看示例以转义特殊字符-

    using Intuit.Ipp.Core;
    using Intuit.Ipp.Data;
    using Intuit.Ipp.LinqExtender;
    using Intuit.Ipp.QueryFilter;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    
    static class SampleCalls
    {
    public static Customer QueryCustomerByDisplayName(ServiceContext context, string     displayName)     {
    displayName = displayName.Replace("'", "\\'"); //Escape special characters
    QueryService<Customer> customerQueryService = new QueryService<Customer>(context);
    return customerQueryService.Where(m => m.DisplayName == displayName).FirstOrDefault();
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-29
      • 1970-01-01
      • 1970-01-01
      • 2011-07-01
      • 2015-11-05
      • 2020-12-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多