Reputation: 385
I'm currently trying to write into the BigCommerce oauth API, but I am continually getting the following error:
The remote server returned an error: (400) Bad Request.
I would like to return a listing of orders/customers/etc. Any suggestions would be appreciated.
A sample of my code is below.
string baseURL = "https://api.bigcommerce.com/stores/" + storeHash + "/v2/" + resourcePath;
string contentType = "application/json";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(baseURL);
req.AllowAutoRedirect = true;
req.ContentType = contentType;
req.Method = "POST";
req.Headers.Add("X-Auth-Client", clientID);
req.Headers.Add("X-Auth-Token", token);
string sendRequest = "min_id=1";
byte[] postData = Encoding.UTF8.GetBytes(sendRequest);
req.ContentLength = postData.Length;
//Send the data to login server
using (Stream stream = req.GetRequestStream()) {
stream.Write(postData, 0, postData.Length);
stream.Flush();
stream.Close();
}
//Get the request response object
WebResponse resp = req.GetResponse();
//Read the contents of the response
StreamReader sr = new StreamReader(resp.GetResponseStream());
string jsonResponse = sr.ReadToEnd();
Upvotes: 1
Views: 1944
Reputation: 385
Thanks to your comments, I was able to come up with this resolution:
//BigCommerce Authorization//
string clientID = "grmx4ghnscp8pz2jb4bf2qylt9b61yh";
string clientSecret = "ibjh6whikzab0jm84zgjw5uydstx1b8";
string token = "d56i5khv6l4kgcd9k92wpavyh93iqh";
string storeHash = "pdcnmnye";
string resourcePath = "products";
string baseURL = "https://api.bigcommerce.com/stores/" + storeHash + "/v2/" + resourcePath;
string requestParams = "?min_id=65&max_id=75";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(baseURL + requestParams);
req.AllowAutoRedirect = true;
req.ContentType = "application/json";
req.Accept = "application/json";
req.Method = "GET";
//string authValue = Convert.ToBase64String(Encoding.Default.GetBytes("admin:" + ""));
req.Headers.Add("X-Auth-Client", clientID);
req.Headers.Add("X-Auth-Token", token);
//req.Headers.Add("Authorization", authValue);
Trace.Write("Base URL",baseURL);
string jsonResponse = null;
using (WebResponse resp = req.GetResponse()) {
if (req.HaveResponse && resp != null) {
using (var reader = new StreamReader(resp.GetResponseStream())) {
jsonResponse = reader.ReadToEnd();
}
}
}
Response.Write(jsonResponse);
Upvotes: 0
Reputation: 61
Is your request a properly formatted querystring? Such as .../v2/orders?min_id=1&max_id=10.
Upvotes: 1