How to add products in Magento 2 using Magento 2 REST API.
I am using http://127.0.0.1/mag/rest/default/V1/products this url in postman but it gives me this error:
{
"message": "The consumer isn't authorized to access %resources.",
"parameters": {
"resources": "Magento_Catalog::products"
},
"trace": "#0 /var/www/html/reg-dealers/vendor/magento/module-webapi/Controller/Rest/RequestValidator.php(68): Magento\\Webapi\\Controller\\Rest\\RequestValidator->checkPermissions()\n#1 /var/www/html/reg-dealers/vendor/magento/module-webapi/Controller/Rest/InputParamsResolver.php(79): Magento\\Webapi\\Controller\\Rest\\RequestValidator->validate()\n#2 /var/www/html/reg-dealers/vendor/magento/framework/Interception/Interceptor.php(58): Magento\\Webapi\\Controller\\Rest\\InputParamsResolver->resolve()\n#3 /var/www/html/reg-dealers/vendor/magento/framework/Interception/Interceptor.php(138): Magento\\Webapi\\Controller\\Rest\\InputParamsResolver\\Interceptor->___callParent('resolve', Array)\n#4 /var/www/html/reg-dealers/vendor/magento/framework/Interception/Interceptor.php(153): Magento\\Webapi\\Controller\\Rest\\InputParamsResolver\\Interceptor->Magento\\Framework\\Interception\\{closure}()\n#5 /var/www/html/reg-dealers/generated/code/Magento/Webapi/Controller/Rest/InputParamsResolver/Interceptor.php(26): Magento\\Webapi\\Controller\\Rest\\InputParamsResolver\\Interceptor->___callPlugins('resolve', Array, Array)\n#6 /var/www/html/reg-dealers/vendor/magento/module-webapi/Controller/Rest/SynchronousRequestProcessor.php(85): Magento\\Webapi\\Controller\\Rest\\InputParamsResolver\\Interceptor->resolve()\n#7 /var/www/html/reg-dealers/vendor/magento/module-webapi/Controller/Rest.php(188): Magento\\Webapi\\Controller\\Rest\\SynchronousRequestProcessor->process(Object(Magento\\Framework\\Webapi\\Rest\\Request\\Proxy))\n#8 /var/www/html/reg-dealers/vendor/magento/framework/Interception/Interceptor.php(58): Magento\\Webapi\\Controller\\Rest->dispatch(Object(Magento\\Framework\\App\\Request\\Http))\n#9 /var/www/html/reg-dealers/vendor/magento/framework/Interception/Interceptor.php(138): Magento\\Webapi\\Controller\\Rest\\Interceptor->___callParent('dispatch', Array)\n#10 /var/www/html/reg-dealers/vendor/magento/framework/Interception/Interceptor.php(153): Magento\\Webapi\\Controller\\Rest\\Interceptor->Magento\\Framework\\Interception\\{closure}(Object(Magento\\Framework\\App\\Request\\Http))\n#11 /var/www/html/reg-dealers/generated/code/Magento/Webapi/Controller/Rest/Interceptor.php(26): Magento\\Webapi\\Controller\\Rest\\Interceptor->___callPlugins('dispatch', Array, Array)\n#12 /var/www/html/reg-dealers/vendor/magento/framework/App/Http.php(116): Magento\\Webapi\\Controller\\Rest\\Interceptor->dispatch(Object(Magento\\Framework\\App\\Request\\Http))\n#13 /var/www/html/reg-dealers/vendor/magento/framework/App/Bootstrap.php(263): Magento\\Framework\\App\\Http->launch()\n#14 /var/www/html/reg-dealers/index.php(42): Magento\\Framework\\App\\Bootstrap->run(Object(Magento\\Framework\\App\\Http\\Interceptor))\n#15 {main}"
}
So how will i add product using rest api as any user.
2 Answers 2
It is not possible to create products as any user, you need an auth token that has adequate permissions.
I normally create a token for an admin user by making the following request:
POST /rest/default/V1/integration/admin/token with the body:
{
"username": "your_username",
"password": "your_password"
}
This will return a token that you pass in the HTTP header:
Authorization: Bearer <authentication token from previous response>
-
will you please tell me when i insert the product it is required to run command php bin/magento indexer:reindex to show product because it will show out of stock when i initially insert it.Prits– Prits2021年03月19日 16:43:41 +00:00Commented Mar 19, 2021 at 16:43
-
1Ideally your cron should be set to run every minute. As long as your indexers are set to "on schedule" mode the cron should re-index any invalidated or new records. If this is the case then you will not need to run it. If you are developing locally and have not configured the cron then you can run the indexer manually but you can also set the indexer to "on save". This will be slower to save but should immediately index new/updated items.Craig– Craig2021年03月19日 16:53:40 +00:00Commented Mar 19, 2021 at 16:53
-
Thanks for the answer @craig but if i have 10 million products in my store then it will take time to reindex all the data. Do you have any idea if any guest user has any product in their cart is there any way to specify that which product is in cart.Prits– Prits2021年03月19日 16:59:25 +00:00Commented Mar 19, 2021 at 16:59
You need to be authorized. Depending on the version of Magento that you're on, you may not be able to generate a token via the api without 2 factor auth.
You can get a token from the admin panel by going to System > Extensions > Integration > Add New Integration and when you see the keys, use the access token as your bearer token in the Authorization tab in postman
-
will you please tell me when i insert the product it is required to run command php bin/magento indexer:reindex to show product because it will show out of stock when i initially insert it and not run the command.Prits– Prits2021年03月19日 16:44:07 +00:00Commented Mar 19, 2021 at 16:44
Explore related questions
See similar questions with these tags.