gpt4 book ai didi

Magento REST API,用户授予管理员角色,但仅授予访客访问权限

转载 作者:行者123 更新时间:2023-12-02 18:27:25 26 4
gpt4 key购买 nike

我正在用 python 编写一个应用程序,它使用 OAuth/REST 等访问 Magento 服务器。

OAuth 身份验证已完成,我有两个消费者 token 和 2 个访问 token 。在 Magento 本身中,我遵循了许多博客中概述的配置步骤 - 设置 REST 角色、属性和使用者以及用户权限和角色。我已经检查了 500 次(感觉就是这样!)并且看不到任何错误,用户正在使用已授权 token 的 REST 使用者,用户的角色是管理员,等等。

在完成 OAuth 流程后,我尝试将产品发布到 Magento(其数据库为空)并收到 403 访问被拒绝时,我注意到出现了问题。 Get 尝试收到相同的结果。我为 Guest 启用了 REST API 访问,现在 Get 收到一个空的 json 数组,当然 Post 仍然有 403 - 这告诉我 Magento 没有查看 OAuth token 并让我登录。

Magento 似乎拒绝接受它在 OAuth 身份验证过程中生成的消费者/访问 token 。

是否有我错过的配置步骤,或者是否有任何信息可以提供克服此障碍的方法?

编辑:下面添加的代码片段显示了我用来查询 Magento 的方法:-

from rauth.session import OAuth1Session
session = OAuth1Session(consumer_key, consumer_secret, access_key, access_secret)
headers = {'Content-Type': 'application/json', 'Accept': 'application/json'}
r = session.get('http://mysite.com/api/rest/products', headers=headers)
print r.json()

输出:{u'messages':{u'error':[{u'message':u'访问被拒绝',u'code':403}]}}

最佳答案

经过几个小时的思考这个问题,我终于意识到为什么我们中的一些人会遇到以下错误:

Invalid auth/bad request (got a 403, expected HTTP/1.1 20X or a redirect) 
{"messages":{"error":[{"code":403,"message":"Access denied"}]}}

即使在成功授权管理员/客户之后。

不幸的是,问题不在于 Magento,而很可能是您的服务器配置。

我开始研究 Magento Api2 代码,并意识到他们正在利用 Zend_Controller_Request_Http 方法“getHeader”来检查 oAuth 授权。

以下是“getHeader()”的代码

public function getHeader($header)
{
if (empty($header)) {
#require_once 'Zend/Controller/Request/Exception.php';
throw new Zend_Controller_Request_Exception('An HTTP header name is required');
}

// Try to get it from the $_SERVER array first
$temp = 'HTTP_' . strtoupper(str_replace('-', '_', $header));
if (isset($_SERVER[$temp])) {
return $_SERVER[$temp];
}

// This seems to be the only way to get the Authorization header on
// Apache
if (function_exists('apache_request_headers')) {
$headers = apache_request_headers();
if (isset($headers[$header])) {
return $headers[$header];
}
$header = strtolower($header);
foreach ($headers as $key => $value) {
if (strtolower($key) == $header) {
return $value;
}
}
}

return false;
}

由 Magento 类“Mage_Api2_Model_Auth_Adapter_Oauth”传递给此函数的是:

public function isApplicableToRequest(Mage_Api2_Model_Request $request)
{
$headerValue = $request->getHeader('Authorization');

return $headerValue && 'oauth' === strtolower(substr($headerValue, 0, 5));
}

由于“$request->getHeader('Authorization') 被调用,这意味着 getHeader 函数中的 $header var 是“Authorization”

// Try to get it from the $_SERVER array first
$temp = 'HTTP_' . strtoupper(str_replace('-', '_', $header));
if (isset($_SERVER[$temp])) {
return $_SERVER[$temp];
}

然后,Zend 类将“Authorization”转换为“HTTP_AUTHORIZATION”,并在 $_SERVER 数组中查找它,这就是问题所在,因为在该数组中,Authorization 值位于“$_SERVER['Authorization']”中不是“$_SERVER['HTTP_AUTHORIZATION"]”,它们这样做是因为 CONTENT_TYPE 等其他变量存储为 HTTP_CONTENT_TYPE,授权除外。

但是,Zend 似乎意识到了这一点,因此他们编写了以下代码:

// This seems to be the only way to get the Authorization header on
// Apache
if (function_exists('apache_request_headers')) {
$headers = apache_request_headers();
if (isset($headers[$header])) {
return $headers[$header];
}
$header = strtolower($header);
foreach ($headers as $key => $value) {
if (strtolower($key) == $header) {
return $value;
}
}
}

这里的问题是,许多服务器没有启用“apache_request_headers”,如果您遇到此问题,则可能是您的托管公司禁用了“apache_request_headers”。

这里有两种解决方案:

  1. 联系您的主机并要求他们启用“apache_request_headers”。
  2. 修改 Zend_Controller_Request_Http 类中的 getHeader($header) 函数

替换:

$temp = 'HTTP_' . strtoupper(str_replace('-', '_', $header));

与:

$temp = ($header == 'Authorization') ? $header : 'HTTP_' . strtoupper(str_replace('-', '_', $header));

关于Magento REST API,用户授予管理员角色,但仅授予访客访问权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18148145/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com