magento - 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 ) )
解决方案
推荐阅读
- ios - React Native - iOS 发布模式下无法访问联系人、日历
- c++ - 无法将辅助表单显示为 MainForm
- c++ - 对“Inventory::insertEnd(Node*, int)”的未定义引用
- python - Pyinstaller:使用 --hiddenimports 标志时出现权限错误
- html - 切换后显示离子菜单但不可点击
- java - 如何遍历字符串 ArrayList 并为字符串 ArrayList 中的每个单词创建临时字符数组
- modifier - 在“ExtendedProperties”之类的修饰符中添加的 certutil 语法是什么
- r - 如何避免这个 Shiny uiOutput 错误?
- c# - 在按钮单击 xamarin 时动态发送 id
- python - selenium.common.exceptions.WebDriverException:消息:服务意外退出。状态码为:3221225595