错误“远程服务器返回错误:(400)错误请求”

时间:2017-02-07 11:09:28

标签: c# asp.net windows quickbooks quickbooks-online

我正在尝试从我们的Windows窗体发送请求到快速预订服务器。但它告诉我错误。我该怎么办?。任何帮助将不胜感激。  这是我试图提出请求的代码:

 var oAuthConsumerKey = "";
 var oAuthConsumerSecret = "";
 var oAuthUrl ="https://quickbooks.api.intuit.com/v3/company/<companyid>/invoice?minorversion=4";

    // Do the Authenticate
 var authHeaderFormat = "Basic {0}";

 var authHeader = string.Format(authHeaderFormat,Convert.ToBase64String(Encoding.UTF8.GetBytes(Uri.EscapeDataString(oAuthConsumerKey) + ":"+Uri.EscapeDataString((oAuthConsumerSecret))) ));

    string text = System.IO.File.ReadAllText("text file");
    //  var postBody = "grant_type=client_credentials";

    HttpWebRequest authRequest = (HttpWebRequest)WebRequest.Create(oAuthUrl);
    authRequest.Headers.Add("Authorization", authHeader);
    authRequest.Method = "POST";
    authRequest.ContentType = "application/x-www-form-urlencoded";
    authRequest.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 7.1; Trident/5.0)";
    authRequest.Accept = "/";
    authRequest.UseDefaultCredentials = true;
    authRequest.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
    authRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

   using (Stream stream = authRequest.GetRequestStream())
    {
        byte[] content = ASCIIEncoding.ASCII.GetBytes(text);
        stream.Write(content, 0, content.Length);
    }

   authRequest.Headers.Add("Accept-Encoding", "gzip");

   WebResponse authResponse = authRequest.GetResponse();

1 个答案:

答案 0 :(得分:1)

您正尝试使用HTTP Basic授权访问Intuit的服务:

var authHeaderFormat = "Basic {0}";

 var authHeader = string.Format(authHeaderFormat,Convert.ToBase64String(Encoding.UTF8.GetBytes(Uri.EscapeDataString(oAuthConsumerKey) + ":"+Uri.EscapeDataString((oAuthConsumerSecret))) ));

Intuit不支持HTTP Basic身份验证。 Intuit支持OAuth。

您是否阅读过文档?来自Intuit网站:

  

QuickBooks API使用OAuth 1.0为应用提供对数据的访问权限   用户的QuickBooks公司。

您还可以参考官方OAuth规范:

我建议您使用已经为您实现所有这些内容的库。 Intuit在他们的网站上链接了几个: