<?php
class Vashi_Restapi_TestController extends Mage_Core_Controller_Front_Action {
public function indexAction() {
//Basic parameters that need to be provided for oAuth authentication
//on Magento
$params = array(
'siteUrl' => Mage::getBaseUrl().'/oauth',
'requestTokenUrl' => Mage::getBaseUrl().'/initiate',
'accessTokenUrl' => Mage::getBaseUrl().'/oauth/token',
'consumerKey' => '3157bsm22p5q80t7zcujfi9uzxafefic',//Consumer key registered in server administration
'consumerSecret' => 'zo22y43t1d63okihubfgkg39rdg3pjvl',//Consumer secret registered in server administration
'callbackUrl' => Mage::getBaseUrl().'/restapi/test/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' => Mage::getBaseUrl().'/oauth',
'requestTokenUrl' => Mage::getBaseUrl().'/initiate',
'accessTokenUrl' => Mage::getBaseUrl().'/oauth/token',
'consumerKey' => '3157bsm22p5q80t7zcujfi9uzxafefic',
'consumerSecret' => 'zo22y43t1d63okihubfgkg39rdg3pjvl'
);
// 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(Mage::getBaseUrl().'/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;
}
}