1

I am trying to use Magento's rest services, trying to follow the example on http://inchoo.net/magento/configure-magento-rest-and-oauth-settings/ and http://inchoo.net/magento/consuming-magento-rest-zend_oauth_consumer/. I have a Magento store installed on localhost over WAMP server.

Basically what I did is:

  1. Add user roles, create oAuth consumer, and enable access to attributes, in Magento's admin, as explained in the link
  2. I created a new module, under app/code/local/Inchoo, named RestConnect, with the folders 'Block', 'controllers', 'etc', 'Helper', 'Model', 'sql'.
  3. Inside 'controllers' folder, I created a file named TestController (previously I had named it IndexController), with the following code:

    <?php
    
    class Inchoo_RestConnect_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' => 'http://127.0.0.1/mystore/index.php/oauth',
        'requestTokenUrl' => 'http://127.0.0.1/mystore/index.php/oauth/initiate',
        'accessTokenUrl' => 'http://127.0.0.1/mystore/index.php/oauth/token',
        'authorizeUrl' => 'http://127.0.0.1/mystore/index.php/admin/oAuth_authorize',//This URL is used only if we authenticate as Admin user type
        'consumerKey' => 'h7n8qjybu78cc3n8cdd5dr7ujtl33uqh',//Consumer key registered in server administration
        'consumerSecret' => '2smfjx37a6e4w23jlcrya6iyv5v32fxr',//Consumer secret registered in server administration
        'callbackUrl' => 'http://127.0.0.1/mystore/index.php/restconnect/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' => 'http://127.0.0.1/mystore/index.php/oauth',
        'requestTokenUrl' => 'http://127.0.0.1/mystore/index.php/oauth/initiate',
        'accessTokenUrl' => 'http://127.0.0.1/mystore/index.php/oauth/token',
        'consumerKey' => 'h7n8qjybu78cc3n8cdd5dr7ujtl33uqh',
        'consumerSecret' => '2smfjx37a6e4w23jlcrya6iyv5v32fxr'
    );
    
    // 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('127.0.0.1/mystore/index.php/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;
     }
    
     }
    
  4. Inside 'etc' folder, I created config.xml, with the following code:

    <config>    
    <modules>
        <Inchoo_RestConnect>
            <version>0.1.0</version>
        </Inchoo_RestConnect>
    </modules>
    
  5. And finally, in app/etc/modules, I created Inchoo_RestConnect.xml:

    <config>
    <modules>
        <Inchoo_RestConnect>
            <active>true</active>
            <codePool>local</codePool>
        </Inchoo_RestConnect>
    </modules>
    

Then I tried to access http://127.0.0.1/mystore/index.php/api/rest/test, as is said in the link, but I don't get the expected result.

I am new to using Magento and REST, and I'm aware I could be missing or misunderstanding something; could you please tell me if you see any error, or I'm missing something in the process?

Thank you in advance :-)

3
  • You may have already fixed this, but I noticed your xml files didn't have a closing </config> tag. Apr 12, 2017 at 14:49
  • Thanks for your answer, but I actually have the closing tags, however I didn't paste them here by mistake.
    – LeCodeur
    Apr 18, 2017 at 7:51
  • 1
    No probs. I actually managed to get this working, but I'm away from my computer til next week so I can't post it. However, i think it was to do with oAuth needing to be lowercase (oauth). I noticed you have it camel case where it says "authorizeUrl". Something to do with cases anyway - that's what solved my problem! :) Apr 18, 2017 at 15:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.