1

I have magento2.1 installed in my server under folder magento2. So base Url is http://$domain/magento2/

After this I have created a new role as admin and under permission clicked All.Then I created a new user with username, password and other details. Then connected the new user to new role.

After that i call the admin/token by passing username and password as

$apiURL="http://".$domain."/magento2/index.php/rest/V1/integration/admin/token";

//parameters passing with URL
$data = array("username" => "username", "password" => "!pass");
$data_string = json_encode($data);

$ch = curl_init($apiURL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Content-Length: ".strlen($data_string)));
print_r(curl_getinfo($ch));
$token = curl_exec($ch);

//decoding generated token and saving it in a variable
echo $token=  json_decode($token,true);

This gave a token but on print the http_code it gives 0. I tried through Postman too.

dadtaqm9b5bjqr6tk35hj8b6iy8a6hou //token

Then i called the customer/me endpoint

$token= trim($token);
//Using above token into header
$headers = array("Authorization: Bearer ".$token,"Accept:application/json");

//API URL to get all Magento 2 modules
$requestUrl='http://'.$domain.'/magento2/index.php/rest/V1/customers/me';

$ch = curl_init($requestUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

//print_r(curl_getinfo($ch));
$result = curl_exec($ch);

//decoding result
$result=  json_decode($result);

//printing result
print_r($result);

This gave output as

stdClass Object ( [message] => Consumer is not authorized to access %resources [parameters] => stdClass Object ( [resources] => self ) )

Any solution to this problem? New role

Role permission

user

Logita Kurrey
  • 71
  • 1
  • 10

0 Answers0