Skip to main content

Rest api oauth token empty

I'm trying to use REST API access. I followed all the guides found to enable the api and oauth functionalities. I'm using this code to retrieve the access and then make the request

<?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;
    }
}

I tried also the admin, customers and guest access but the result is always:

An error occurred. Your authorization request is invalid.

It seems the oauth token is not being generated as the url I can see is:

http://magentohost/oauth/authorize/?oauth_token=&oauth_callback=http%3A%2F%2Fmagentohost%2F%2Frestapi%2Ftest%2Fcallback

Really struggling with it, any advice? thanks

apedic
  • 923
  • 12
  • 37