Created
April 15, 2014 05:20
-
-
Save mathewka/10704227 to your computer and use it in GitHub Desktop.
Magento Rest api call from controller with using Oauth
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Namespace_modulename_IndexController extends Mage_Core_Controller_Front_Action{ | |
public function indexAction() { | |
//Basic parameters that need to be provided for oAuth authentication | |
//on Magento | |
$params = array( | |
'siteUrl' => 'http://localhost/magento/oauth', | |
'requestTokenUrl' => 'http://localhost/magento/oauth/initiate', | |
'accessTokenUrl' => 'http://localhost/magento/oauth/token', | |
'authorizeUrl' => 'http://localhost/magento/admin/oAuth_authorize',//This URL is used only if we authenticate as Admin user type | |
'consumerKey' => 'zz6bygmkqnn9z6xzi7wsx87pt7s0vvtd',//Consumer key registered in server administration | |
'consumerSecret' => '43y4ntjs5jjz9x3wu6t8n327fswy1onv',//Consumer secret registered in server administration | |
'callbackUrl' => 'http://localhost/magento/api/rest/products',//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://localhost/magento/oauth', | |
'requestTokenUrl' => 'http://localhost/magento/oauth/initiate', | |
'accessTokenUrl' => 'http://localhost/magento/oauth/token', | |
'consumerKey' => 'zz6bygmkqnn9z6xzi7wsx87pt7s0vvtd', | |
'consumerSecret' => '43y4ntjs5jjz9x3wu6t8n327fswy1onv' | |
); | |
// 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://localhost/magento/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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment