通过BigCommerce restful API从商店中提取类别

时间:2013-10-03 15:08:17

标签: c# restful-authentication bigcommerce

我想通过API在BigCommerce上从我的商店中提取类别。

当我在API控制台上尝试我的凭据时; https://developer.bigcommerce.com/console

它工作正常,但我通过C#发送请求发送凭据时它不起作用。

我收到了[{"status":401,"message":"No credentials were supplied in the request."}]

我的代码就像;

List<PostCredential> postCredentials = new List<PostCredential>();
postCredentials.Add(new PostCredential { name = "store_url", value = ApiPath });
postCredentials.Add(new PostCredential { name = "username", value = Username });
postCredentials.Add(new PostCredential { name = "api_key", value = ApiToken });
JavaScriptSerializer serializer = new JavaScriptSerializer();
string serialize = serializer.Serialize(postCredentials);
const string requestUrl = "https://myteststore1234.mybigcommerce.com/api/v2/categories.json";

StringBuilder postData = new StringBuilder();
postData.Append(serialize);
string postRequest = PostRequest(requestUrl, postData.ToString());

public static string PostRequest(string url, string postData)
{
  HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(url);
  ASCIIEncoding encoding = new ASCIIEncoding();
  byte[] data = encoding.GetBytes(postData);
  httpWReq.Method = "POST";
  httpWReq.ContentType = "application/x-www-form-urlencoded";
  httpWReq.ContentLength = data.Length;
  using (Stream stream = httpWReq.GetRequestStream())
  {
    stream.Write(data, 0, data.Length);
  }
  HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();
  Stream responseStream = response.GetResponseStream();
  if (responseStream != null)
  {
    string responseString = new StreamReader(responseStream).ReadToEnd();
    return responseString;
  }
  return null;
}

有没有解决方法呢?

2 个答案:

答案 0 :(得分:1)

        WebHeaderCollection wHeader = new WebHeaderCollection();

        wHeader.Clear();
        //wHeader.Add("username:test");
        //wHeader.Add("password:4afe2a8a38fbd29c32e8fcd26dc51f6d9b5ab99b");
        //wHeader.Add("u","test:4afe2a8a38fbd29c32e8fcd26dc51f6d9b5ab99b");
        string Username = "test";
        string ApiToken = "4afe2a8a38fbd29c32e8fcd26dc51f6d9b5ab99b";


        string sUrl = "https://store-bwvr466.mybigcommerce.com/api/v2/brands.json";  //txtstoreurl.Text.ToString() + "/api/v2/products.json";

        HttpWebRequest wRequest = (HttpWebRequest)System.Net.HttpWebRequest.Create(sUrl);
        wRequest.Credentials = new NetworkCredential(Username, ApiToken);

        wRequest.ContentType = "application/json"; //' I don't know what your content type is
        //wRequest.Headers = wHeader;
        wRequest.Method = "GET";

        HttpWebResponse wResponse = (HttpWebResponse)wRequest.GetResponse();

        string sResponse = "";

        using (StreamReader srRead = new StreamReader(wResponse.GetResponseStream()))
        {
            sResponse = srRead.ReadToEnd();
            MessageBox.Show(sResponse);
        }

答案 1 :(得分:0)

经过一些研究后我发现我需要传递这样的参数;

httpWReq.Method = "GET";
httpWReq.ContentType = "application/json";
httpWReq.ContentLength = data.Length;
httpWReq.Credentials = new NetworkCredential(Username, ApiToken);