【发布时间】:2016-11-30 15:39:56
【问题描述】:
我正在尝试通过以下链接使 Magento REST API 示例正常工作(创建一个简单的产品作为具有 OAuth 身份验证的管理员用户):
http://devdocs.magento.com/guides/m1x/api/rest/introduction.html
但是我收到以下错误:
在弄清楚如何安装 OAuth 之后,我花了很长时间才走到这一步,这部分变得非常痛苦,因为我找不到任何解决这些错误的方法。
有人有什么想法吗?如果需要,我的代码如下:
<?php
$callbackUrl = "http://localhost/API.php";
$temporaryCredentialsRequestUrl = "https://ts564737-container.zoeysite.com/oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
$adminAuthorizationUrl = 'https://ts564737-container.zoeysite.com/admin/oauth_authorize';
$accessTokenRequestUrl = 'https://ts564737-container.zoeysite.com/oauth/token';
$apiUrl = 'https://ts564737-container.zoeysite.com/api/rest';
$consumerKey = '526ced0202719d14951e1849016d6b3d';
$consumerSecret = 'a06606b73962a0efbafea32af3d89380';
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";
$productData = json_encode(array(
'type_id' => 'simple',
'attribute_set_id' => 4,
'sku' => 'simple' . uniqid(),
'weight' => 1,
'status' => 1,
'visibility' => 4,
'name' => 'Simple Product',
'description' => 'Simple Description',
'short_description' => 'Simple Short Description',
'price' => 99.95,
'tax_class_id' => 0,
));
$headers = array('Content-Type' => 'application/json');
$oauthClient->fetch($resourceUrl, $productData, OAUTH_HTTP_METHOD_POST, $headers);
print_r($oauthClient->getLastResponseInfo());
}
} catch (OAuthException $e) {
print_r($e);
}
感谢您提供任何见解。
【问题讨论】:
标签: php rest api magento magento-1.9