gpt4 book ai didi

api - 401 错误 "oauth_problem=nonce_used"使用 Rest API 将产品添加到 Magento

转载 作者:行者123 更新时间:2023-12-02 04:05:07 25 4
gpt4 key购买 nike

尝试使用其余 API 将产品添加到 Magento 时,获取 401 状态并返回“oauth_problem=nonce_used”消息。奇怪的是,产品仍然被导入,但这真的让我失望,因为我没有得到产品 ID 来更新库存信息。

Magento 安装是全新的(crucialwebhost 安装程序)1.7.0.2,我使用的代码几乎是从 magento 站点复制和粘贴的...

$callbackUrl = '****';
$temporaryCredentialsRequestUrl = "*****/oauth/initiate?oauth_callback=".urlencode($callbackUrl);
$adminAuthorizationUrl = '*****/admin/oauth_authorize';
$accessTokenRequestUrl = '*****/oauth/token';
$apiUrl = '*****/api/rest';

$consumerKey = '*****';
$consumerSecret = '******';

try
{
$authType = ($_SESSION['state'] == 2) ? OAUTH_AUTH_TYPE_AUTHORIZATION : OAUTH_AUTH_TYPE_URI;
$oauthClient = new OAuth($consumerKey, $consumerSecret, OAUTH_SIG_METHOD_HMACSHA1, $authType);
$oauthClient->enableDebug();

if(!isset($_GET['oauth_token']) && !$_SESSION['state'])
{
$requestToken = $oauthClient->getRequestToken($temporaryCredentialsRequestUrl);
$_SESSION['secret'] = $requestToken['oauth_token_secret'];
$_SESSION['state'] = 1;
header('Location: '.$adminAuthorizationUrl.'?oauth_token='.$requestToken['oauth_token']);
exit;
} else if($_SESSION['state'] == 1)
{
$oauthClient->setToken($_GET['oauth_token'], $_SESSION['secret']);
$accessToken = $oauthClient->getAccessToken($accessTokenRequestUrl);
$_SESSION['state'] = 2;
$_SESSION['token'] = $accessToken['oauth_token'];
$_SESSION['secret'] = $accessToken['oauth_token_secret'];
header('Location: '.$callbackUrl);
exit;
} else
{
$oauthClient->setToken($_SESSION['token'], $_SESSION['secret']);
$resourceUrl = "$apiUrl/products";


$productData = json_encode(array(
'type_id' => 'simple',
'attribute_set_id' => 4,
'sku' => $local_product['sku'],
'weight' => 1,
'status' => 1,
'visibility' => 4,
'name' => $local_product['name'],
'description' => $local_product['description'],
'short_description' => $local_product['description'],
'price' => $local_product['price'],
'tax_class_id' => 0,
));
$headers = array('Content-Type' => 'application/json');
$oauthClient->fetch($resourceUrl, $productData, OAUTH_HTTP_METHOD_POST, $headers);
$respHeader = $oauthClient->getLastResponseHeaders();


}

} catch(OAuthException $e)
{
print_r($e);
}
}

session_destroy();

确切错误:{"messages":{"error":[{"code":401,"message":"oauth_problem=nonce_used"}]}}

最佳答案

在Mage_Api2_Model_Resource中,大约第227行,找到

$this->getResponse()->setHeader('Location', $newItemLocation);

并在此之后插入:

 $this->getResponse()->setHttpResponseCode(202); 

引用:维基百科“HTTP 位置”:

The HTTP Location header field is returned in responses from an HTTP server under two circumstances:

  1. To ask a web browser to load a different web page. In this circumstance, the Location header should be sent with an HTTP status code of 3xx.
  2. To provide information about the location of a newly created resource. In this circumstance, the Location header should be sent with an HTTP status code of 201 or 202

关于api - 401 错误 "oauth_problem=nonce_used"使用 Rest API 将产品添加到 Magento,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15772102/

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