0

I can able to get token from http://example.com/magento2/rest/V1/integration/admin/token with Username & Password.

Using this token, I can request product details on http://example.com/magento2/rest/V1/products/:sku and get stock level are working.

When I try to get a order details, I am getting error like:

"message": "Consumer is not authorized to access %resources",
"parameters": 
    "resources": "Magento_Sales::sales"

When I try to get a customer details, I am getting error like:

"message": "Consumer is not authorized to access %resources",
"parameters": 
    "resources": "Magento_Customer::customer"

I can access only functions which is vendor/magento/module-moduleName/etc/webapi.xml with resource ref="anonymous".

Rest of them I am having same error "Consumer is not authorized to access %resources".

How can able to access all magento core function through rest api?

1
  • I copied this magento to new temporary server. Everything working fine. I am wondering what is wrong with my server. I can able to access all modules I wanted.
    – maniramav
    May 15, 2018 at 15:53

4 Answers 4

1

Right now you are able to access only that api which can be accessed anonymously. Now you are accessing a api which need a authentication key/access token to assure that you are a registered customer who is accessing such api's. So for generating you have to generate a access token using below api :

{base_url}/rest/V1/integration/customer/token

this is a post type api, so you have to pass the username and password in the body,

{
"username":"{your_username}",
"password":"{password}"
}

and hit it, you will get a access token like this :

"6fomgh65em5jaivxqllorc50h2gqrhhe"

Now you have to use this access token in the api you are currently trying to access : In Postman -> go to header and in key = Authorization and in value = Bearer {your generated token} and hit SEND

DONE, That's All.

2
  • But this way will limit me from accessing all modules. Doesn't it?
    – maniramav
    May 9, 2018 at 12:01
  • no, in this way you can access all the api's using token which need customer token, or the api's which have set <resource ref="self" />.
    – Deepak
    May 23, 2018 at 8:42
1

There are two types of authentication available in magento 2. One is via admin another is via customer. You can use below api to get the authenticated token which will be used to run all api having ref="self".

{base_url}/rest/V1/integration/customer/token
{base_url}/rest/V1/integration/admin/token

Your request will be of POST type and param will be

{
"username":"{your_username}", 
"password":"{password}"
}

You have to use your admin login credentials for admin auth token or customer login credentials for customer token.

After getting the authentication token, you need to pass it while calling the api in basic authorisation

Authorization : Bearer cy50YW5kb25AaGVhbHRoY29hdXN0cmFsaWEuY29tLmF1OmhlYWx0aGNvMjAxNw==

something like above

4
  • My authentication is working fine, problem with my server.
    – maniramav
    May 15, 2018 at 15:56
  • anywhere else I can help you?
    – Vivek
    May 17, 2018 at 9:09
  • this seems to work for me Jun 13, 2018 at 10:59
  • i am trying to use newly created token on (rest/V1/inventory/source-items) API but it only works if i define token as static, if i generate a new token at run time and pass in API then it gives authorized error... can you please guide
    – Alam Zaib
    Apr 23, 2019 at 9:21
0

You will get customer order by getting access token by using

http://example.com/magento2/rest/V1/integration/customer/token 

with customer username & Password.

After that By using that access token You get customer order because this api only accessible for authenticate customer.

0

I updated whole server including kernel then I rebooted. Now it is working fine.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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