WooCommerce Rest API 返回 404 not found [英] WooCommerce Rest API returns 404 not found
问题描述
我正在尝试使用 REST API 从 WooCommerece 中提取一定数量的产品:
I am trying to pull a count of products from WooCommerece using the REST API:
http://www.skyverge.com/woocommerce-rest-api-docs.html#authentication/over-https
我可以访问此页面并收到有效的 JSON:
I can visit this page and I receive valid JSON:
https://[OMITTED].com/wc-api/v1
https://[OMITTED].com/wc-api/v1
当我访问/wc-api/v1/products/count 时,我收到 404 错误:
When I visit /wc-api/v1/products/count, I get a 404 error:
{"errors":[{"code":"woocommerce_api_authentication_error","message":"Consumer Key is missing"}]}
{"errors":[{"code":"woocommerce_api_authentication_error","message":"Consumer Key is missing"}]}
在 WooCommerce > 设置 > 结帐下,已启用安全结帐.我的代码不正确吗?它适用于新的 2.1 安装.
Under WooCommerce > Settings > Checkout, secure checkout has been enabled. Is my code incorrect? It works fine with a new 2.1 install.
这是我的代码:
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
推荐答案
您需要在请求 url 中包含 oauth_consumer_key.
You need to include the oauth_consumer_key in your request url.
登录您的 WP-Admin 并转到您的用户个人资料以生成您的 API 密钥.您应该会在 Woocommerce API 密钥部分看到一些包含消费者密钥和消费者秘密的内容.
Login into your WP-Admin and go to your user profile to generate your API keys. You should see something with consumer key and consumer secret, in section Woocommerce API Keys.
获得消费者密钥的值后,您的示例请求将是:
After you get the value for the consumer key your sample request will be:
http 或 https://yourdomain.com/wc-api/v2/products/?oauth_consumer_key=ck_670ae128f1ebb1859ba03b2171f8ca7c
希望能帮到你
这篇关于WooCommerce Rest API 返回 404 not found的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!