0

我正在尝试通过 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;
}

有什么解决方法吗?

4

2 回答 2

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);
        }
于 2013-10-05T15:03:56.753 回答
0

经过一番研究,我发现我需要像这样传递参数;

httpWReq.Method = "GET";
httpWReq.ContentType = "application/json";
httpWReq.ContentLength = data.Length;
httpWReq.Credentials = new NetworkCredential(Username, ApiToken);
于 2013-10-03T15:40:52.313 回答