我正在尝试使用REST API从WooCommerece中提取产品数量:
http://www.skyverge.com/woocommerce-rest-api-docs.html#authentication/over-https
我可以访问此页面并收到有效的JSON:
的https:// [省略]的.com / WC-API / V1
当我访问/ wc-api / v1 / products / count时,我收到404错误:
{"错误":[{"代码":" woocommerce_api_authentication_error","消息":"消费者密钥丢失& #34;}]}
在WooCommerce>设置>结账,安全结账已启用。我的代码不正确吗?它在新的2.1安装下运行良好。
这是我的代码:
public bool Authenticate()
{
try
{
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(m_BaseUrl + "/products/count");
//myHttpWebRequest.Accept = "text/xml";
string userP = m_UserName + ":" + m_Password;
byte[] authBytes = Encoding.UTF8.GetBytes(userP).ToArray();
myHttpWebRequest.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(authBytes));
WebResponse httpResponse = myHttpWebRequest.GetResponse();
Stream responseStream = httpResponse.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
string resultData = reader.ReadToEnd();
responseStream.Close();
httpResponse.Close();
return true;
}
catch (Exception)
{
return false;
}
}
GET https://www.[OMITTED].com/wc-api/v1/products/count HTTP/1.1
Authorization: Basic [OMITTED]
Host [OMITTED]
Connection: Keep-Alive
答案 0 :(得分:1)
您需要在请求网址中包含oauth_consumer_key。
登录您的WP-Admin并转到您的用户个人资料以生成您的API密钥。您应该在Woocommerce API Keys部分中看到具有消费者密钥和消费者秘密的内容。
获得使用者密钥的值后,您的样本请求将是:
http或https://yourdomain.com/wc-api/v2/products/?oauth_consumer_key=ck_670ae128f1ebb1859ba03b2171f8ca7c
希望这有帮助