WooCommerce Rest API returns 404 not found

2020-07-11 09:42发布

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

I can visit this page and I receive valid JSON:

https://[OMITTED].com/wc-api/v1

When I visit /wc-api/v1/products/count, I get a 404 error:

{"errors":[{"code":"woocommerce_api_authentication_error","message":"Consumer Key is missing"}]}

Under WooCommerce > Settings > Checkout, secure checkout has been enabled. Is my code incorrect? It works fine with a new 2.1 install.

Here is my code:

    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

1条回答
叼着烟拽天下
2楼-- · 2020-07-11 10:04

You need to include the oauth_consumer_key in your request url.

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 or https://yourdomain.com/wc-api/v2/products/?oauth_consumer_key=ck_670ae128f1ebb1859ba03b2171f8ca7c

Hope this helps

查看更多
登录 后发表回答