0

请找到以下用于过滤账单的代码,即使我通过了 resultperpage>10,它也只返回 10 条排序记录。

public IEnumerable<Intuit.Ipp.Data.Qbo.Bill> FilterBills(DataServices dataServices, int startPage, int resultsPerPage, DateTime After, DateTime Before)
    {
Intuit.Ipp.Security.OAuthRequestValidator oAuthRequestValidator = ((Intuit.Ipp.Security.OAuthRequestValidator)dataServices.ServiceContext.RequestValidator);

            OAuthConsumerContext consumerContext = new OAuthConsumerContext
            {
                ConsumerKey = oAuthRequestValidator.ConsumerKey,
                SignatureMethod = SignatureMethod.HmacSha1,
                ConsumerSecret = oAuthRequestValidator.ConsumerSecret
            };

            OAuthSession oSession = new OAuthSession(consumerContext, "https://oauth.intuit.com/oauth/v1/get_request_token",
                        "https://workplace.intuit.com/Connect/Begin",
                        "https://oauth.intuit.com/oauth/v1/get_access_token");

            oSession.AccessToken = new TokenBase
            {
                Token = oAuthRequestValidator.AccessToken,
                ConsumerKey = oAuthRequestValidator.ConsumerKey,
                TokenSecret = oAuthRequestValidator.AccessTokenSecret
            };

            var body = "PageNum={0}&ResultsPerPage={1}&Filter=LastUpdatedTime :AFTER: {2} :AND: LastUpdatedTime :BEFORE: {3} & Sort=LastUpdatedTime HighToLow";
            body = String.Format(body, startPage, resultsPerPage, After.ToString("yyyy-mm-ddTHH:MM:ssz"), Before.ToString("yyyy-mm-ddTHH:MM:ssz"));

            IConsumerRequest conReq = oSession.Request();
            conReq = conReq.Post().WithRawContentType("application/x-www-form-urlencoded").WithRawContent(System.Text.Encoding.ASCII.GetBytes(body)); ;
            conReq = conReq.ForUrl(dataServices.ServiceContext.BaseUrl + "bills/v2/" + dataServices.ServiceContext.RealmId);
            conReq = conReq.SignWithToken();

            Intuit.Ipp.Data.Qbo.SearchResults searchResults = (Intuit.Ipp.Data.Qbo.SearchResults)dataServices.ServiceContext.Serializer.Deserialize<Intuit.Ipp.Data.Qbo.SearchResults>(conReq.ReadBody());
            IEnumerable<Intuit.Ipp.Data.Qbo.Bill> SearchResult = ((Intuit.Ipp.Data.Qbo.Bills)(searchResults.CdmCollections)).Bill;
            return SearchResult;
 }

发送请求后,我将响应作为 10 个已排序的账单记录而没有出现任何错误。我正在使用 IPDotNetDevKit 2.1.12.0 版本。

请查看此过滤问题。

4

1 回答 1

1

在上面的示例中,OAuth 参数在请求正文中传递并覆盖您的请求正文,因此它默认为 PageNum=1&ResultsPerPage=20。此外,日期格式不正确。这是一个可以工作的代码示例。

public IEnumerable<Intuit.Ipp.Data.Qbo.Bill> FilterBills(DataServices dataServices, int startPage, int resultsPerPage, DateTime After, DateTime Before)
{
    StringBuilder requestXML = new StringBuilder();
    StringBuilder responseXML = new StringBuilder();

    var requestBody =
        "PageNum={0}&ResultsPerPage={1}&Filter=LastUpdatedTime :AFTER: {2} :AND: LastUpdatedTime :BEFORE: {3}&Sort=LastUpdatedTime HighToLow";
    requestBody = String.Format(requestBody, startPage, resultsPerPage, After.ToString("yyyy-MM-ddThh:mm:sszzz"),
                            Before.ToString("yyyy-MM-ddThh:mm:sszzz"));

    HttpWebRequest httpWebRequest =
        WebRequest.Create(dataServices.ServiceContext.BaseUrl + "bills/v2/" +
                            dataServices.ServiceContext.RealmId) as HttpWebRequest;
    httpWebRequest.Method = "POST";
    httpWebRequest.ContentType = "application/x-www-form-urlencoded";
    httpWebRequest.Headers.Add("Authorization", GetDevDefinedOAuthHeader(httpWebRequest, requestBody));
    requestXML.Append(requestBody);
    UTF8Encoding encoding = new UTF8Encoding();
    byte[] content = encoding.GetBytes(requestXML.ToString());
    using (var stream = httpWebRequest.GetRequestStream())
    {
        stream.Write(content, 0, content.Length);
    }
    HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
    using (Stream data = httpWebResponse.GetResponseStream())
    {
        Intuit.Ipp.Data.Qbo.SearchResults searchResults =
            (Intuit.Ipp.Data.Qbo.SearchResults)
            dataServices.ServiceContext.Serializer.Deserialize<Intuit.Ipp.Data.Qbo.SearchResults>(
                new StreamReader(data).ReadToEnd());
        return ((Intuit.Ipp.Data.Qbo.Bills) searchResults.CdmCollections).Bill.ToList();
    }
}


private string GetDevDefinedOAuthHeader(HttpWebRequest webRequest, string requestBody)
{

    OAuthConsumerContext consumerContext = new OAuthConsumerContext
    {
        ConsumerKey = consumerKey,
        ConsumerSecret = consumerSecret,
        SignatureMethod = SignatureMethod.HmacSha1,
        UseHeaderForOAuthParameters = true

    };

    consumerContext.UseHeaderForOAuthParameters = true;

    OAuthSession oSession = new OAuthSession(consumerContext, "https://www.example.com",
                            "https://www.example.com",
                            "https://www.example.com");


    oSession.AccessToken = new TokenBase
    {
        Token = accessToken,
        ConsumerKey = consumerKey,
        TokenSecret = accessTokenSecret
    };

    IConsumerRequest consumerRequest = oSession.Request();
    consumerRequest = ConsumerRequestExtensions.ForMethod(consumerRequest, webRequest.Method);
    consumerRequest = ConsumerRequestExtensions.ForUri(consumerRequest, webRequest.RequestUri);
    if (webRequest.Headers.Count > 0)
    {
        ConsumerRequestExtensions.AlterContext(consumerRequest, context => context.Headers = webRequest.Headers);
        if (webRequest.Headers[HttpRequestHeader.ContentType] == "application/x-www-form-urlencoded")
        {
            Dictionary<string, string> formParameters = new Dictionary<string, string>();
            foreach (string formParameter in requestBody.Split('&'))
            {
                formParameters.Add(formParameter.Split('=')[0], formParameter.Split('=')[1]);
            }
            consumerRequest = consumerRequest.WithFormParameters(formParameters);
        }
    }

    consumerRequest = consumerRequest.SignWithToken();
    return consumerRequest.Context.GenerateOAuthParametersForHeader();
}
于 2013-03-21T20:06:33.133 回答