问题是使用REST API Magento

时间:2015-02-11 22:18:09

标签: php magento oauth

我一直在努力通过REST API连接到我的Magento商店。 我试过连接作为一个独立程序从XAMPP运行:这是代码:

<?php
$callbackUrl = "http://healthandmed.com/oauth_admin.php";
$temporaryCredentialsRequestUrl =
"http://healthandmed.com/oauth/initiate?oauth_callback=" .
urlencode($callbackUrl);
$adminAuthorizationUrl = 'http://healthandmed.com/admin123/oauth_authorize';
$accessTokenRequestUrl = 'http://healthandmed.com/oauth/token';
$apiUrl = 'http://healthandmed.com/api/rest';
$consumerKey = 'xxxxxxxxxxx';
$consumerSecret = 'xxxxxxxxxxxxx';

session_start();
if (!isset($_GET['oauth_token']) && isset($_SESSION['state']) &&
$_SESSION['state'] == 1) {
   $_SESSION['state'] = 0;
}
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";
       $oauthClient->fetch($resourceUrl);
       $productsList = json_decode($oauthClient->getLastResponse());
       echo count($productsList);
   }
} catch (OAuthException $e) {
   print_r($e);
}
    ?>

我也尝试过Inchoo的教程: /消耗-magento的静止-zend_oauth_consumer /

两种解决方案都将我带到了同一个地方: http://healthandmed.com/admin123/oauth_authorize
带有以下错误消息: “发生错误。您的授权请求无效。”

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我认为问题在于您的管理员授权网址,即 $ adminAuthorizationUrl ,网址必须为http://healthandmed.com/admin/oauth_authorize,请替换“admin123”#39;与&#39; admin&#39;。执行此操作时,系统将提示您登录magento admin后端,然后系统会要求您进行授权。