Search code examples
woocommercewoocommerce-rest-apidokan

Dokan REST API return 401 error, where is problem?


Based on Dokan documents, I use woocommerce API consumer_key and consumer_secret in Dokan APIs, but it returns "rest_forbidden" status 401 error, while there is not any problem when use woocommerce APIs.

I use following endpoints:

the first one return 401 error and second one return json responses.

https://....com/wp-json/dokan/v1/products/123/?consumer_key=...&consumer_secret=... https://....com/wp-json/wc/v3/products/123/?consumer_key=...&consumer_secret=...

Where is the problem? can any one help?


Solution

  • I didn't find in the Dokan documentation that you can use WooCommerce Rest credentials.

    But it is possible with this filter

    add_filter( 'woocommerce_rest_is_request_to_rest_api', 'add_dokan_to_rest_api' );
    
    function add_dokan_to_rest_api($is_request){
        if($is_request){
            return $is_request;
        }
        if ( empty( $_SERVER['REQUEST_URI'] ) ) {
            return false;
        }
        $rest_prefix = trailingslashit( rest_get_url_prefix() );
        $request_uri = esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) );
    
        return ( false !== strpos( $request_uri, $rest_prefix . 'dokan/' ) );
    }