Vote count:
0
How can I get data including the product information and customer information that must be authorized through the Megento REST API ?I followed the guide book step by step but still don't know how to do. First,I created a new consumer at Admin Panel according the guide book.
- On the Magento Admin Panel menu, select System > Web Services > REST - OAuth Consumers.
- On the OAuth Consumers page, click Add New in the top right corner to add a new consumer.
- The New Consumer page opens. The Key and Secret fields are filled automatically and cannot be edited. These values are generated automatically and will be used to identify the Consumer in Magento.
Then,I built a oauth_admin.php at my host root like this.
<?php
$callbackUrl = "http://ift.tt/18KghBl";
$temporaryCredentialsRequestUrl = "http://ift.tt/18KghBp" . urlencode($callbackUrl);
$adminAuthorizationUrl = 'http://ift.tt/19jESOt';
$accessTokenRequestUrl = 'http://ift.tt/18KghRD';
$apiUrl = 'http://ift.tt/19jESOv';
$consumerKey = 'e94b6b9c113d4903aaab4f08c8150754';
$consumerSecret = 'fcf5bdf58bc36c434e52f1ac916ba5ae';
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']);
header('Location: ' . $adminAuthorizationUrl . '?oauth_token=' . $requestToken['oauth_token'].'&oauth_token_secret='.$requestToken['oauth_token_secret']);
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, array(), 'GET', array('Content-Type' => 'application/json'));
$productsList = json_decode($oauthClient->getLastResponse());
print_r($productsList);
}
} catch (OAuthException $e) {
print_r($e->getMessage());
echo "<br/>";
print_r($e->lastResponse);
}
?>
After that , I tried visiting myhost/oauth_admin.php. This address response a 404 but I can get the oauth_token and oauth_token_secret at the redirect link like this.
And then,I copied the oauth_token and oauth_token_secret and tried using the REST-Client to send a request to the myhost/api/rest/stockitems but returned a 401.
<?xml version="1.0"?>
<magento_api>
<messages>
<error>
<data_item>
<code>401</code>
<message>oauth_problem=consumer_key_rejected</message>
</data_item>
</error>
</messages>
</magento_api>
My environment can be found at http://ift.tt/19jESOx I am a beginner to magento. I wondered what mistakes had I made during the process.How can I get the data from the RestAPI? I would very much appreciate it if any on could help.
How can I get data through the Megento REST API
Aucun commentaire:
Enregistrer un commentaire