Coder Social home page Coder Social logo

Comments (5)

kiatng avatar kiatng commented on June 12, 2024 1

As requested:

    /**
     * REST API Client TEST
     * @link https://inchoo.net/magento/consuming-magento-rest-zend_oauth_consumer/
     */

    protected $_hostIndex = 0;
    protected $_hosts = [
        [
            'https://somedomain.com.my',
            'fa2d6a11353e5c0c58e179ad00070d64', // consumerKey
            '1c2dd1edd26f7f2413a19fe1449c1b21', // consumerSecret
        ],
        [
            'https://somedomain2.com.my',
            '2ec27fd255f0ef5e90ba47502c9c377a',
            'badb9ace83677378141860fa13a52b23',
        ]
    ];

    protected function _getOauthParams($withCallbackUrl = true): array
    {
        $host = $this->_hosts[$this->_hostIndex][0];
        $consumerKey = $this->_hosts[$this->_hostIndex][1];
        $consumerSecret = $this->_hosts[$this->_hostIndex][2];

        $params = [
            'siteUrl' => "$host/oauth",
            'requestTokenUrl' => "$host/oauth/initiate",
            'accessTokenUrl' => "$host/oauth/token",
            'authorizeUrl' => "$host/oauth/authorize", // frontend customers
            //'authorizeUrl' => "$host/admin/oauth_authorize", // admn users
            'consumerKey' => $consumerKey,
            'consumerSecret' => $consumerSecret,
        ];
        if ($withCallbackUrl) {
            $params['callbackUrl'] = Mage::getUrl('your_module/your_controllerName/callback');
        }
        return $params;
    }

    // acceess: /callback/?oauth_token=a3dc8163449897e5535518ed8e2ee8f6&oauth_verifier=5538f558be99e80d405aac4f282fd2eb
    // rejected: /callback/?oauth_token=d94a4934f78f0f53f60b4ed755c01951&rejected=1
    public function callbackAction()
    {
        //$session = Mage::getSingleton('core/session');
        // Read and unserialize request token from session
        //$requestToken = $session->getOauthRequestToken();
        $requestToken = unserialize(base64_decode(Mage::getModel('core/flag', ['flag_code' => 'request_token'])
            ->loadSelf()
            ->getFlagData()));
        $params = $this->_getOauthParams();
        $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("{$this->_hosts[$this->_hostIndex][0]}/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(json_decode($response->getBody(), true));

        return;
    }

    /**
     * Entry point.
    */
    public function oauthAction()
    {
        $consumer = new Zend_Oauth_Consumer($this->_getOauthParams());
        $requestToken = $consumer->getRequestToken();

        // Save serialized request token object in session for later use
        //Mage::getSingleton('core/session')->setOauthRequestToken($requestToken);
        Mage::getModel('core/flag', ['flag_code' => 'request_token'])
            ->loadSelf()
            ->setFlagData(base64_encode(serialize($requestToken)))
            ->save();
        // Redirect to authorize URL
        $consumer->redirect();
    }

from magento-lts.

Flyingmana avatar Flyingmana commented on June 12, 2024

my guess is it was either only in the enterprise edition, or exclusive to some modules implementing oauth.

from magento-lts.

fballiano avatar fballiano commented on June 12, 2024

I checked an M1.14 enterprise that I've here and there's nothing regarding that event :-(

from magento-lts.

sreichel avatar sreichel commented on June 12, 2024

Any code (or extension) to reproduce for lazy people like me`?

from magento-lts.

sreichel avatar sreichel commented on June 12, 2024

Thanks.

from magento-lts.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.