0

I am playing with magento 2 Rest API to add category in magento admin. I am writing below code, but showing "401 Unauthorized" and showing message Consumer is not authorized to access %resources Magento_Catalog::categories

I am using below code to add category in magento console, can anyone help me on this? Thanks in advance.

POST Method: [MAGENTO URL]/index.php/rest/V1/categories/

Request Body: { "id": 0, "parentId": 0, "name": "test category", "isActive": true, "position": 0, "level": 0, "includeInMenu": true, "extensionAttributes": {}, }

but not working. any solution?

Thanks

Rahul Anand.

  • This API has access to all resources. – Rahul Dec 10 '15 at 5:10
1

Follow the steps defined in authentication guide provided by Magento.

http://devdocs.magento.com/guides/v2.0/get-started/authentication/gs-authentication.html

This guide is more elaborated and helpful in achieving your requirements. Magento team has added more details in it and it will help you out in authenticating your request.

You can use any method:
- Token based
- Oauth based
- Session based

2

The problem is you are trying to access protected admin resource without authentication.

The easiest option for you would be to get access token using the following request:

curl -X POST "https://magento.host/index.php/rest/V1/integration/admin/token" \
     -H "Content-Type:application/json" \
     -d '{"username":"test@example.com", "password":"123123q"}'

Then specify this token (e.g. vbnf3hjklp5iuytre) in Authotization header while making requests to protected resources, e.g.:

curl -X GET "http://magento.ll/index.php/rest/V1/customers/2" \
     -H "Authorization: Bearer vbnf3hjklp5iuytre"

Also there is no need to specify "id": 0 in request for new category creation.

Please take a look at the Getting started guide for Magento 2 web APIs.

  • hi Dude, Thanks for your help. But we are trying to run rest api code, and it keep showing that error "Consumer is not authorized to access %resources". Can you help me understanding how can we fix it? – Rahul Dec 10 '15 at 9:36
  • Assuming that you generated access token for the admin user, the only problem could be that the admin does not have permissions to manage categories. Check that your user is assigned to role, which has access to this resource in System => Permissions => All Users (UserRoles) – Alex Paliarush Dec 10 '15 at 9:39
  • In the example I provided you get token for Admin user, not integration. So permissions should be configured for the admin user whose username/password you specified during token request. – Alex Paliarush Dec 10 '15 at 9:44
  • Hi,user is created as admin, and role is there which can access all resources. Still getting same error. – Rahul Dec 10 '15 at 10:06
  • I am using Firefox rest client to make request. All credentials are given there, and also the consumer key and token too. – Rahul Dec 10 '15 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.