Intuit IPP Rest API 查询字符串转义 [英] Intuit IPP Rest API query string escape
问题描述
因此,对于 Intuit IPP Rest API,
假设我想查询一个名为 ABC 的客户,我可以使用这样的 http get 请求
Say if i want to query a customer who's name is ABC, i can use a http get request like this
https://qb.sbfinance.intuit.com/v3/company/198445012/query?query=select Id from Customer where FullyQualifiedName%3D'ABC'
&3D 是 '=' 的 url 转义,这没有任何问题.
&3D is a url escape of '=', this works without any problem.
现在如果客户的名字是 A&B,我尝试了这样的查询字符串
Now if the customer's name is A&B, I tried the query string like this
select Id from Customer where FullyQualifiedName='A&B'
url编码后是这个样子
After the url encoding, it looks like this
https://qb.sbfinance.intuit.com/v3/company/198445012/query?query=select Id from Customer where FullyQualifiedName%3D'A%26B'
它会失败.
有什么想法吗?
更新
以上网址是我从 IPP 的 API 浏览器中复制的.
The above urls i copied from the IPP's API explorer.
这是代码,我使用的是 DevDefined.OAuth
Here is the code, I am using 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数据是这样的
the returned xml data like this
<?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 失败.但这是我今天早上得到的.
I am not 100% sure, yesterday when i test, it actually return something says the oauth failed. But this is what I got this morning.
实际上,您可以在 IPP 的 API 资源管理器中尝试它,它给出了相同的结果.
Actually, you can try it within IPP's API explorer, it gives the same result.
ForUrl 的 devdefined 代码
The devdefined's code for ForUrl
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 &或部分数据,所以我更改了以下行
The real issue is Uri(url) won't escape the & in 'A&B' because it doesn't know if it is a url & or part of the data, So i changed the following line
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 对数据查询字符串进行转义.
use Uri.EscapeDataString to escape the data query string first.
推荐答案
如果您创建名为A&B"的客户,则 V3 服务会返回该客户对象,如下所示 -
If you create a customer with name 'A&B', then V3 service returns that customer object like below -
<?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>
要按名称检索此对象,您需要使用以下查询
To retrieve this object by name, you need to use the following query
SELECT * FROM Customer WHERE DisplayName = 'A&B'
但是它需要像下面这样进行url编码
But it needs to be url encoded like following
SELECT+*+FROM+Customer+WHERE+DisplayName+%3D+%27A%26B%27
实现此目的的 Java 代码 -
Java code to achieve this -
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 可以很好地处理所有这些)
It works perfectly. (Devkit handles all these pretty well )
希望这能回答你的问题.
Hope this answers your qts.
谢谢
这篇关于Intuit IPP Rest API 查询字符串转义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!