Magento 2 REST API

问题描述 投票:0回答:1

我正在尝试为Magento REST API创建一个node.js客户端。我使用在我自己的centOS服务器上运行的社区版。

我遵循使用api的步骤:

  • 在Magento管理面板中创建具有管理员角色的用户
  • 创建新的集成
  • 激活集成(在此之后,我收到ConsumerKey,ConsumerSecret,AccessToken,SecretToken)

问题是:当我尝试获取任何端点的get请求时,我得到:

{
Client is not authorized to access this resource: ResourceName
}

请求是:

request({
      url: this.url+endpoint,
      method: 'GET',
      headers:{
        'Authorization': 'Bearer '+bearer,
        'User-Agent': '...',
        'Accept' : 'application/json',
        'Content-Type': 'application/json'
      }
    }, (error, response, body) => {
      callback(error,response,body)
    })

})

我通过此请求正确地获得了持有人(我检查过)令牌:

request({
  url: this.url+'integration/admin/token',
  method: 'POST',
  json:{
    username: this.username,
    password: this.password
  }
}, (error, response, body) => {
    callback(body)
})

浏览magento安装,我遇到了授权请求:

 public function isAllowed($resource, $privilege = null)
{
    return $this->_aclPolicy->isAllowed($this->_aclRoleLocator->getAclRoleId(), $resource, $privilege);
}

如果我改变

return true

我无需身份验证即可访问所有资源,但这不是我想要的

我检查了这个功能

getAclRoleId()

我找到了这个DefaultRoleLocator.php

namespace Magento\Framework\Authorization\RoleLocator;

class DefaultRoleLocator implements 
\Magento\Framework\Authorization\RoleLocatorInterface
{
/**
 * Retrieve current role
 *
 * @return string
 */
public function getAclRoleId()
{
    return '';

}
}

我不是很擅长PHP,而且我是magento的新手。即使是oauth身份验证也失败了。

我要改变这个功能吗?我必须创建另一个RoleLocator类?

任何帮助将非常感激 !

php node.js rest magento magento2
1个回答
0
投票

您确定添加了访问所请求资源的权限吗? permissions

© www.soinside.com 2019 - 2024. All rights reserved.