我一直在努力通过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尝试过这个教程:/consuming-magento-rest-zend_oauth_consumer/
两种解决方案都让我走到了同一个地方:http://healthandmed.com/admin123/oauth_authorize
并显示以下错误消息:"出现错误。您的授权请求无效。"
任何帮助都将不胜感激。
我认为问题出在您的管理授权Url上,即$adminAuthorizationUrlUrl必须是http://healthandmed.com/admin/oauth_authorize,将"admin123"替换为"admin"。当你这样做时,你会被提示登录到magento管理后端,然后你会被要求授权。