所以,对于 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: <EOF> after : "\'A"</Detail>
</Error>
</Fault>
</IntuitResponse>
我不是 100% 确定,昨天当我测试时,它实际上返回了一些内容,表明 oauth 失败。但这是我今天早上得到的。
实际上,您可以在IPP的API资源管理器中尝试一下,它会给出相同的结果。
ForUrl 的 devdefine 代码
public static IConsumerRequest ForUrl(this IConsumerRequest request, string url)
{
request.Context.RawUri = new Uri(url);
return request;
}
这会将 url 编码为
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对数据查询字符串进行转义。
最佳答案
如果您创建名为“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&B</FullyQualifiedName>
<DisplayName>A&B</DisplayName>
<PrintOnCheckName>A&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 很好地处理了所有这些)
希望这能解答您的疑问。
谢谢
关于api - Intuit IPP Rest API 查询字符串转义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22517452/