cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2 - consumer is not authorized to access %resources - Randomly getting this error in REST

Magento 2 - consumer is not authorized to access %resources - Randomly getting this error in REST

I am getting above error randomly - Previously its working - but suddenly It starting to give above error.

Please help 

Tags (1)
4 REPLIES

Re: Magento 2 - consumer is not authorized to access %resources - Randomly getting this error in RES

//Authentication rest API magento2.Please change url accordingly your url
$adminUrl='http://localhost/index.php/rest/V1/integration/admin/token';
$ch = curl_init();
$data = array("username" => "wsname", "password" => "m7Craqa5");
$data_string = json_encode($data);
$ch = curl_init($adminUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$token = curl_exec($ch);
$token= json_decode($token);

$ch = curl_init("http://localhost/index.php/rest/V1/customers/1");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization:Bearer " . $token));

$result = curl_exec($ch);

var_dump($result);

 

highlighted line is very important . Where I was making mistake was "not putting space after bearer"

Re: Magento 2 - consumer is not authorized to access %resources - Randomly getting this error in RES

Hi @amitkadam, I would guess this random errors can appear after your session (auth token) expires.

Ideally, I would reauthorize on catching such error. However, as a temporaryy fix, you can as well increase session lifetime.

Re: Magento 2 - consumer is not authorized to access %resources - Randomly getting this error in RES

I have the same problem
I have tried with different versions of php and Magento
Nothing works, any API post with administrator token fails
"Consumer is not authorized to access% resources"

On local server works perfectly, but in production cpanel server no
Any ideas ?

Re: Magento 2 - consumer is not authorized to access %resources - Randomly getting this error in RES

Same trouble. Doesn't work on cpanel. 

Fresh installation(2.2.3), there is only one admin account with access to everything and I use its credentials to create admin token. And yes - there is space after Bearer. It simply doesn't work.