I am using Magento 1.9.2 vesrion for. Using below code for calling rest api.
public function indexAction() {
//Basic parameters that need to be provided for oAuth authentication
//on Magento
$params = array(
'siteUrl' => 'http://127.0.0.1:8080/magentodemo/',
'requestTokenUrl' => 'http://127.0.0.1:8080/magentodemo/oauth/initiate',
'accessTokenUrl' => 'http://127.0.0.1:8080/magentodemo/oauth/token',
'authorizeUrl' => 'http://127.0.0.1:8080/magentodemo/admin/oauth_authorize',//This URL is used only if we authenticate as Admin user type
'consumerKey' => 'c359b57d0d069a336db94fa4aabd61ce',//Consumer key registered in server administration
'consumerSecret' => '2ef029c871b7c013619cc15445a83c25',//Consumer secret registered in server administration
'callbackUrl' => 'http://127.0.0.1:8080/magentodemo/restconnect/index/callback',//Url of callback action below
);
// Initiate oAuth consumer with above parameters
$consumer = new Zend_Oauth_Consumer($params);
// Get request token
$requestToken = $consumer->getRequestToken();
// Get session
$session = Mage::getSingleton('core/session');
// Save serialized request token object in session for later use
$session->setRequestToken(serialize($requestToken));
// Redirect to authorize URL
$consumer->redirect();
return;
}
public function callbackAction() {
//oAuth parameters
$params = array(
'siteUrl' => 'http://127.0.0.1:8080/magentodemo/',
'requestTokenUrl' => 'http://127.0.0.1:8080/magentodemo/oauth/initiate',
'accessTokenUrl' => 'http://127.0.0.1:8080/magentodemo/oauth/token',
'consumerKey' => 'c359b57d0d069a336db94fa4aabd61ce',
'consumerSecret' => '2ef029c871b7c013619cc15445a83c25'
);
// Get session
$session = Mage::getSingleton('core/session');
// Read and unserialize request token from session
$requestToken = unserialize($session->getRequestToken());
// Initiate oAuth consumer
$consumer = new Zend_Oauth_Consumer($params);
// Using oAuth parameters and request Token we got, get access token
$acessToken = $consumer->getAccessToken($_GET, $requestToken);
// Get HTTP client from access token object
$restClient = $acessToken->getHttpClient($params);
// Set REST resource URL
$restClient->setUri('http://127.0.0.1:8080/magentodemo/api/rest/products');
// In Magento it is neccesary to set json or xml headers in order to work
$restClient->setHeaders('Accept', 'application/json');
// Get method
$restClient->setMethod(Zend_Http_Client::GET);
//Make REST request
$response = $restClient->request();
// Here we can see that response body contains json list of products
Zend_Debug::dump($response);
return;
}
Getting below error every time
a:5:{i:0;s:83:"Could not retrieve a valid Token response from Token URL:
oauth_problem=nonce_used";i:1;s:1453:"#0 D:\xampp\htdocs\magentodemo\lib\Zend\Oauth\Http.php(190): Zend_Oauth_Http->_assessRequestAttempt(Object(Zend_Http_Response))
#1 D:\xampp\htdocs\magentodemo\lib\Zend\Oauth\Http.php(191): Zend_Oauth_Http->startRequestCycle(Array)
#2 D:\xampp\htdocs\magentodemo\lib\Zend\Oauth\Http.php(191): Zend_Oauth_Http->startRequestCycle(Array)
#3 D:\xampp\htdocs\magentodemo\lib\Zend\Oauth\Http\RequestToken.php(51): Zend_Oauth_Http->startRequestCycle(Array)
#4 D:\xampp\htdocs\magentodemo\lib\Zend\Oauth\Consumer.php(115): Zend_Oauth_Http_RequestToken->execute()
#5 D:\xampp\htdocs\magentodemo\app\code\local\Test\RestConnect\controllers\IndexController.php(52): Zend_Oauth_Consumer->getRequestToken()
#6 D:\xampp\htdocs\magentodemo\app\code\core\Mage\Core\Controller\Varien\Action.php(418): Test_RestConnect_IndexController->indexAction()
#7 D:\xampp\htdocs\magentodemo\app\code\core\Mage\Core\Controller\Varien\Router\Standard.php(254): Mage_Core_Controller_Varien_Action->dispatch('index')
#8 D:\xampp\htdocs\magentodemo\app\code\core\Mage\Core\Controller\Varien\Front.php(172): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#9 D:\xampp\htdocs\magentodemo\app\code\core\Mage\Core\Model\App.php(365): Mage_Core_Controller_Varien_Front->dispatch()
#10 D:\xampp\htdocs\magentodemo\app\Mage.php(684): Mage_Core_Model_App->run(Array)
#11 D:\xampp\htdocs\magentodemo\index.php(83): Mage::run('', 'store')
#12 {main}";s:3:"url";s:31:"/magentodemo/restconnect/index/";s:11:"script_name";s:22:"/magentodemo/index.php";s:4:"skin";s:7:"default";}
I have already created, REST Role for Admin and Assigned to one of the admin user, REST Attribute for Admin, REST Consumers is also created.
Any help will be appreciated.
There was a small fix for above issue. But it takes my 1 day to fix it. In my system apache was running on 8080 port. There is issue in magento oauth to read port while URI validation.
I have stopped other service (IIS or Skype), in my case it was IIS. and port apache on 80. This is the default port.
Hope this will help to someone!!!