2

I was able to get the admin token using the below url against the created admin user http://username:[email protected]/index.php/rest/V1/integration/admin/token

The username and password is for browser authentication which you can see before the url which is defined in .htaccess and .htpassword. The original username and password is given in the body

{
"username":"admin",
"password":"admin123"
}

But when I get customers by passing the token it says 401 unauthorized. Below is the request

http://username:[email protected]/index.php/rest/V1/customers and token is passed as "Authorization: Bearer abcdefghi" in request header

How to fix this issue?

2 Answers 2

4

In magento web-API when you pass user name and password then it genrates token for that specific customer (Which is only valid for 1 hour - configurable from admin)

http://magento.host/index.php/rest/V1/integration/customer/[email protected]&password=test@123

webapi.xml code

<route url="/V1/customers/me" method="GET">
    <service class="Magento\Customer\Api\CustomerRepositoryInterface" method="getById"/>
    <resources>
        <resource ref="self"/>
    </resources>
    <data>
        <parameter name="customerId" force="true">%customer_id%</parameter>
    </data>
</route>

which returns token.

After genrating token, when we pass that token in header.

Authorization :: Bearer <Token Value>

http://magento.host/index.php/rest/V1/customers/me

Which returns customer detailes.

The above case i explained is working fine for webAPI in magento2 which i tested in POSTMAN.

8
  • And yeah your URL action should be "POST" May 21, 2018 at 10:21
  • I have created one admin user and new role with which will be "web api role" with access rights to customers and categories. I have assigned the "web api role" to admin user. It means that this admin user can only access the customers and categories. Now I have successfully generate the token. But when I use that token to fetch the customers. It says 401 unauthorized. I have gone some many resources, I understand the concept but don't know why it is keep on giving 401 unauthorized
    – JN_newbie
    May 21, 2018 at 10:43
  • Okay, just for testing purpose please give all access role to that user and check whether it's giving a same error. May 21, 2018 at 10:50
  • and did you checked the action of the URL ? May 21, 2018 at 10:51
  • It was "GET" before now it is "POST" and still 401. Let me try be giving all access roles
    – JN_newbie
    May 21, 2018 at 11:02
0

Please pass username and password correctly

for customers API: rest/V1/customer/token

2
  • Actually the username and password before url is for browser authentication which is defined in .htaccess. The original admin username and password was provided in body to get the token. Now to fetch the customers I think we will pass that token in header and simple call the url.
    – JN_newbie
    May 21, 2018 at 7:02
  • There is one more thing I have created this new admin api user in production mode with specific role to access customers and categories. Do I need to do something to reflect the change?
    – JN_newbie
    May 21, 2018 at 7:15

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.