whe*_*ker 10 api rest oauth magento
尝试使用其余api向Magento添加产品时,返回获得401状态并显示"oauth_problem = nonce_used"消息.奇怪的是,这些产品仍然是进口的,但它真的让我失望,因为我没有得到更新股票信息的产品ID.
Magento安装是全新的(criticalwebhost安装程序)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();
Run Code Online (Sandbox Code Playgroud)
确切错误:{"messages":{"error":[{"code":401,"message":"oauth_problem = nonce_used"}]}}
小智 7
在Mage_Api2_Model_Resource中,关于第227行,找到
$this->getResponse()->setHeader('Location', $newItemLocation);
Run Code Online (Sandbox Code Playgroud)
并在此之后插入:
$this->getResponse()->setHttpResponseCode(202);
Run Code Online (Sandbox Code Playgroud)
参考:维基百科"HTTP位置":
在两种情况下,HTTP服务器的响应中返回HTTP Location头字段:
- 要求Web浏览器加载不同的网页.在这种情况下,应使用HTTP状态代码3xx发送Location标头.
- 提供有关新创建资源的位置的信息.在这种情况下,应使用HTTP状态代码201或202发送Location标头
| 归档时间: |
|
| 查看次数: |
5493 次 |
| 最近记录: |