V
V
Vladimir Kulikov2020-04-29 09:52:45
WooCommerce
Vladimir Kulikov, 2020-04-29 09:52:45

Why 401 error in rest api?

Hello, I want to add a product via rest api to woocommerce, but I get a 401 error.
This is an error:

stdClass Object
(
    [code] => woocommerce_rest_cannot_create
    [message] => У вас недостаточно прав для создания ресурсов.
    [data] => stdClass Object
        (
            [status] => 401
        )

)


This is the add code.
<?php 

$api_response = wp_remote_post( 'http://localhost/ecler/wp-json/wc/v2/products', array(
  'headers' => array(
     'Authorization' => 'Basic ' . base64_encode( 'cs_d80c14bf929469657155e3e7f8aa446ce4b3016a' )
   ),
   'body' => array(
     'name' => 'My test2', // product title
     'status' => 'publish', // product status, default: publish
     'categories' => array(
       array( 
         'id' => 15 // each category in a separate array
       ),
    //    array(
    // 	   'id' => 10
    //    )
     ),
     'regular_price' => '9.99' // product price
     // more params http://woocommerce.github.io/woocommerce-rest-api-docs/?shell#product-properties
   )
) );

$body = json_decode( $api_response['body'] );
//print_r( $body );

if( wp_remote_retrieve_response_message( $api_response ) === 'Created' ) {
   echo 'The product ' . $body->name . ' has been created';
}

print_r( $body );
// print_r( $api_response );
?>


This is .htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /ecler/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /ecler/index.php [L]
</IfModule>
# END WordPress


What causes a 401 error? Googled, advised to change .htaccess but does not help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kulikov, 2020-04-29
@it_proger29

It turns out here "'Authorization' => 'Basic '. base64_encode( 'cs_d80c14bf929469657155e3e7f8aa446ce4b3016a' )" you need to enter data from the account and not the secret key and site key that is given when creating api in woocommerce -_-

$api_response = wp_remote_post( 'http://localhost/ecler/wp-json/wc/v3/products', array(
  'headers' => array(
     'Authorization' => 'Basic ' . base64_encode( 'ЛОГИН:ПАРОЛЬ' )
   ),
   'body' => array(
     'name' => 'My test2', // product title
     'status' => 'publish', // product status, default: publish
     'categories' => array(
       array( 
         'id' => 15 // each category in a separate array
       ),
    //    array(
    // 	   'id' => 10
    //    )
     ),
     'regular_price' => '9.99' // product price
     // more params http://woocommerce.github.io/woocommerce-rest-api-docs/?shell#product-properties
   )
) );

$body = json_decode( $api_response['body'] );
//print_r( $body );

if( wp_remote_retrieve_response_message( $api_response ) === 'Created' ) {
   echo 'The product ' . $body->name . ' has been created';
}

print_r( $body );
// print_r( $api_response );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question