Magento 2 API始终返回消费者无权访问%resources

Wer*_*ing 5 php rest magento magento2

我正在尝试使用Magento 2 rest API,但是即使只是从命令行卷曲,我得到的唯一响应是 Consumer is not authorized to access %resources

我设法获得了令牌

curl -X POST "http://magento.dev.blackorange.nl/index.php/rest/V1/integration/admin/token" \
-H "Content-Type:application/json" \
-d '{"username":"adminuser", "password":"adminpass"}'`
Run Code Online (Sandbox Code Playgroud)

但是当我尝试

curl -X GET -H "Authorization: Bearer TOKEN" "http://magento.dev.blackorange.nl/rest/V1/products" 
Run Code Online (Sandbox Code Playgroud)

(或任何其他端点)我得到的,该Consumer is not authorized to access %resources消息

我在某处错过了一步吗?

Emi*_*ech 1

确保您使用的是 Magento2 管理员用户名密码而不是任何 API 用户名密码 为该用户分配了正确的角色,如果这不起作用,请尝试更改角色:全部

要访问GET /V1/products,您需要将searchCriteria参数传递给其所需参数。

第一个你需要调用 admin/token API 从这里获得的令牌你需要传入第二个 API

TOKEN替换为第一次调用时的令牌密钥

curl -X GET -H "Authorization: Bearer ntfw****avefg***6mil***kysnl3***" "http://magento.dev.blackorange.nl/rest/V1/products" 
Run Code Online (Sandbox Code Playgroud)

对于所有 REST API 参考: http://devdocs.magento.com/swagger/index.html#/