manish sharma manish sharma - 1 year ago 116
ASP.NET (C#) Question

Error “The remote server returned an error: (400) Bad Request”

I am trying to send request from our windows form to quick book server.But it's show me error.What can i do?.any help would be appreciated.
here is the code where i'm trying to make the request:

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();

Answer Source

You are trying to use HTTP Basic authorization to access Intuit's services:

var authHeaderFormat = "Basic {0}";

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

Intuit does not support HTTP Basic auth. Intuit supports OAuth.

Did you read the documentation? From the Intuit website:

The QuickBooks API uses OAuth 1.0 to give apps access to data in a user's QuickBooks company.

You can also refer to the official OAuth spec here:

I would recommend you use a library which already implements all of this stuff for you. Intuit links several on their website:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download