通过 BigCommerce RESTful API 从商店中提取类别
Posted
技术标签:
【中文标题】通过 BigCommerce RESTful API 从商店中提取类别【英文标题】:Pulling categories from store via BigCommerce restful API 【发布时间】:2013-10-10 08:38:51 【问题描述】:我正在尝试通过 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;
有什么解决办法吗?
【问题讨论】:
究竟是什么不工作? 我收到 ["status":401,"message":"No credentials are provided in the request."] 响应错误。 【参考方案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);
【讨论】:
【参考方案2】:经过一番研究,我发现我需要像这样传递参数;
httpWReq.Method = "GET";
httpWReq.ContentType = "application/json";
httpWReq.ContentLength = data.Length;
httpWReq.Credentials = new NetworkCredential(Username, ApiToken);
【讨论】:
以上是关于通过 BigCommerce RESTful API 从商店中提取类别的主要内容,如果未能解决你的问题,请参考以下文章