首页 > 解决方案 > Megento 2 stdClass Object ( [message] => Consumer 无权访问 %resources [parameters] => stdClass Object ( [resources] => self ) )

问题描述

我在 magento2 文件夹下的服务器中安装了 magento2.1。所以基本网址是 http://$domain/magento2/

在此之后,我创建了一个新角色作为管理员并在权限下单击全部。然后我创建了一个具有用户名、密码和其他详细信息的新用户。然后将新用户连接到新角色。

之后,我通过传递用户名和密码来调用管理员/令牌

$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);

这给出了一个令牌,但在打印 http_code 时它给出了 0。我也尝试过 Postman。

dadtaqm9b5bjqr6tk35hj8b6iy8a6hou //token

然后我打电话给客户/我的端点

$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);

这给出了输出

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

这个问题有什么解决办法吗? 新角色

角色权限

用户

标签: magentomagento2

解决方案


推荐阅读