401错误" oauth_problem = nonce_used"将产品添加到Magento的W / REST API [英] 401 Error "oauth_problem=nonce_used" Adding Products To Magento w/ Rest API

查看:500
本文介绍了401错误" oauth_problem = nonce_used"将产品添加到Magento的W / REST API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获得401状态与oauth_problem = nonce_used消息返回尝试使用REST API将产品添加到Magento的时候。奇怪的是,该产品仍然可以进口,但它的真正扔我了,因为我没有得到该产品的ID在重新更新库存信息。

Magento的安装是全新的(crucialwebhost安装程序)1.7.0.2和我使用code是非常复制和Magento站点粘贴pretty ...

  $ callbackUrl ='****';
$ temporaryCredentialsRequestUrl =***** / OAuth的/启动oauth_callback =?urlen code($ callbackUrl)。
$ adminAuthorizationUrl ='***** /管理/ oauth_authorize';
$ accessTokenRequestUrl ='***** / OAuth的/令牌';
$ apiUrl ='***** / API /休息';$ consumerKey ='*****';
$ consumerSecret ='******';尝试
{
$ =的authType($ _SESSION ['状态'] == 2)? OAUTH_AUTH_TYPE_AUTHORIZATION:OAUTH_AUTH_TYPE_URI;
$ oauthClient =新的OAuth($ consumerKey,$ consumerSecret,OAUTH_SIG_METHOD_HMACSHA1,$的authType);
$ oauthClient-> enableDebug();如果(使用isset($ _ GET ['组oauth_token'])及!&安培;!$ _ SESSION ['状态'])
{
  $ requestToken = $ oauthClient-> getRequestToken($ temporaryCredentialsRequestUrl);
  $ _SESSION ['秘密'] = $ requestToken ['oauth_token_secret'];
  $ _SESSION ['状态'] = 1;
  头('位置:'。'?=组oauth_token'。$ adminAuthorizationUrl $ requestToken ['组oauth_token']);
  出口;
}否则如果($ _ SESSION ['状态'] == 1)
{
  $ oauthClient-> setToken($ _ GET ['组oauth_token'],$ _SESSION ['秘密']);
  $的accessToken = $ oauthClient-> getAccessToken($ accessTokenRequestUrl);
  $ _SESSION ['状态'] = 2;
  $ _SESSION ['令牌'] = $的accessToken ['组oauth_token'];
  $ _SESSION ['秘密'] = $的accessToken ['oauth_token_secret'];
  头('位置:'。$ callbackUrl);
  出口;
}其他
{
  $ oauthClient-> setToken($ _ SESSION ['令牌'],$ _SESSION ['秘密']);
  $ resourceUrl =$ apiUrl /产品;
  $ productData = json_en code(阵列(
TYPE_ID'=> '简单',
    attribute_set_id'=> 4,
    SKU=> $ local_product ['SKU'],
    重量=> 1,
    状态=> 1,
'知名度'=> 4,
    '名'=> $ local_product ['名'],
    '描述'=> $ local_product ['说明'],
    SHORT_DESCRIPTION'=> $ local_product ['说明'],
    '价格'=> $ local_product ['价格'],
    tax_class_id'=> 0,
  ));
  $头=阵列(Content-Type的'=>'应用程序/ JSON');
  $ oauthClient->取($ resourceUrl,$ productData,OAUTH_HTTP_METHOD_POST,$头);
  $ respHeader = $ oauthClient-> getLastResponseHeaders();
}}赶上(OAuthException $ E)
{
  的print_r($ E);
}
}session_destroy();

确切错误:{消息:{错误:[{code:401,消息:oauth_problem = nonce_used}]}}


解决方案

在Mage_Api2_Model_Resource,大约227行,找到

  $这个 - > GETRESPONSE() -  GT;的setHeader('位置',$ newItemLocation);

和刚插入后,这样的:

  $这个 - > GETRESPONSE() -  GT; setHtt presponse code(202);

参考:维基百科HTTP位置


  

HTTP位置头字段是从返回HTTP响应中
  在两种情况下服务器:


  
  

      
  1. 要问一个网页浏览器来加载不同的网页。在这
      情况下,Location头应与HTTP状态发送
      3XX的code。

  2.   
  3. 要提供有关的新的位置信息
      创建资源。在这种情况下,Location头应
      与201或202
  4. HTTP状态code发送
      

Getting a 401 status with "oauth_problem=nonce_used" message return when attempting to add products to Magento using the rest api. Oddly, the products are still get imported but it's really throwing me off because I'm not getting the product id's back in which to update the stock info.

Magento install is brand new (crucialwebhost installer) 1.7.0.2 and the code I'm using is pretty much copied and pasted from magento site...

$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();

Exact error: {"messages":{"error":[{"code":401,"message":"oauth_problem=nonce_used"}]}}

解决方案

In Mage_Api2_Model_Resource, about line 227, locate

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

and insert just after this:

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

Ref: Wikipedia "HTTP Location":

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

这篇关于401错误" oauth_problem = nonce_used"将产品添加到Magento的W / REST API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆