WooCommerce - woocommerce_rest_cannot_view - Status 401

前端 未结 10 928
眼角桃花
眼角桃花 2021-01-02 00:09

I have generated a consumer key and consumer secret. The website has SSL installed. I have also installed plugins required for JSON and REST services. This is how the url lo

10条回答
  •  既然无缘
    2021-01-02 00:34

    Here is a modified answer to Quickredfox's anwer:

    add_filter('woocommerce_rest_check_permissions', 'my_woocommerce_rest_check_permissions', 90, 4);
    
    function my_woocommerce_rest_check_permissions($permission, $context, $object_id, $post_type) {
        if($_GET['consumer_key'] == 'asdfghj' && $_GET['consumer_secret'] == 'qwerty') {
            return true;
        }
    
        return $permission;
    }
    

    The downside to this is that the flexibility of adding and revoking access for users using a gui is lost. However, if nothing else works and you just can't figure out why, this will work and does not expose the API to the whole world.

    Oh, and this requires passing the key and secret as parameters a la:

    https://foo.bar.com/wp-json/wc/v3/products/123&consumer_key=asdfghj&consumer_secret=qwerty
    

    This will work without https, but if you use it without https, remember that any credentials you send along with your request will be sent in plain text.

提交回复
热议问题