1

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?

Web Design
  • 77
  • 2
  • 19

1 Answers1

2

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/' ) );
}
Moshe Gross
  • 1,206
  • 1
  • 7
  • 14
  • Should I use user login id and pass in `'Authorization': 'Basic' + btoa('username:password'),` in `headers`? – Web Design Jun 22 '22 at 13:03
  • No, with this snippet you can use consumer_key and consumer_secret, additionaly you do not need basic auth plugin as it is handled by WooCommerce – Moshe Gross Jun 22 '22 at 13:19
  • Your add_filter worked currectly with c_k and c_s and also you are correct, Dokan don't mention Woocommerce Rest credentials in documentation. In this condition, should I use `'Authorization': 'Basic' + btoa('username:password')` ? – Web Design Jun 22 '22 at 13:28
  • 1
    if you want to use username:password then you can remove this snippet, but you'll need to install the basic auth plugin, on the other hand you can use the above and use c_k and c_s – Moshe Gross Jun 22 '22 at 13:33
  • Thanks Moshe Gross – Web Design Jun 22 '22 at 13:41