cancel
Showing results for 
Search instead for 
Did you mean: 

Consuming Magento REST service using Zend_OAuth_Consumer

Consuming Magento REST service using Zend_OAuth_Consumer

I have a question for you. I test the code below with get request and function very well, but I need do a post request for create a product. I put the follow code but I receive an error message.

 

GET request

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' => 'value',//Consumer key registered in server administration
					'consumerSecret' => 'value',//Consumer secret registered in server administration
					'callbackUrl' => Mage::getModel('adminhtml/url')->getUrl('*/sales_order_callapi/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://localhost/magento/oauth',
					'requestTokenUrl' => 'http://localhost/magento/oauth/initiate',
					'accessTokenUrl' => 'http://localhost/magento/oauth/token',
					'consumerKey' => 'value',
					'consumerSecret' => 'value'
			);
	
			// 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('Content-type', 'application/json');
			// Get method
			$restClient->setMethod(Zend_Http_Client::POST);
			
			
			$restClient->setParameterGet(array(
					'type_id'           => 'simple',
					'attribute_set_id'  => 4,
					'sku'               => 'SKUTEST2' . uniqid(),
					'weight'            => 1,
					'status'            => 1,
					'visibility'        => 4,
					'name'              => 'Simple Product',
					'description'       => 'Simple Description',
					'short_description' => 'Simple Short Description',
					'price'             => 69.95,
					'tax_class_id'      => 0,
			));  
			
			
			//Make REST request
			$response = $restClient->request();
			// Here we can see that response body contains json list of products
			Zend_Debug::dump($response);
	
			return;
		}

POST request

			$restClient->setHeaders('Content-type', 'application/json');
                        $restClient->setMethod(Zend_Http_Client::POST);
                        $restClient->setParameterPost(array(
					'type_id'           => 'simple',
					'attribute_set_id'  => 4,
					'sku'               => 'SKUTEST' . uniqid(),
					'weight'            => 1,
					'status'            => 1,
					'visibility'        => 4,
					'name'              => 'Simple Product',
					'description'       => 'Simple Description',
					'short_description' => 'Simple Short Description',
					'price'             => 69.95,
					'tax_class_id'      => 0,
                        $response = $restClient->request();

The error code is:

object(Zend_Http_Response)[330]
  protected 'version' => string '1.1' (length=3)
  protected 'code' => int 500
  protected 'message' => string 'Internal Server Error' (length=21)
  protected 'headers' => 
    array (size=7)
      'Date' => string 'Thu, 26 Oct 2017 08:50:00 GMT' (length=29)
      'Server' => string 'Apache' (length=6)
      'X-frame-options' => string 'SAMEORIGIN' (length=10)
      'X-powered-by' => string 'PHP/5.6.22' (length=10)
      'Content-length' => string '29' (length=2)
      'Connection' => string 'close' (length=5)
      'Content-type' => string 'text/html; charset=UTF-8' (length=24)
  protected 'body' => string 'Service temporary unavailable' (length=29)

 

I config my admin user of magento who is conected with their code with All resources role and all acl attribute rules.
is the code that I use correct?
Thank you for your support

4 REPLIES 4

Re: Consuming Magento REST service using Zend_OAuth_Consumer

@agroverd

 

The below error comes up in Magento when the file maintenance.flag exist in your Magento root directory. 

protected 'body' => string 'Service temporary unavailable' (length=29)

So if you see this file then remove it. After this you shouldn't be seen this error again.

-- Ravindra

Re: Consuming Magento REST service using Zend_OAuth_Consumer

Thank you for your response Ravindra,

 

I don't have this file in my magento. This file is used for put magento in maintenance mode so I do not think the problem is due to that at all

 

Regards


Re: Consuming Magento REST service using Zend_OAuth_Consumer

Hi @agroverd

 

Check this out, problem seems similar to yours.

 

https://magento.stackexchange.com/questions/166895/how-to-add-product-using-rest-api

Beside this, make your that all required fields like 

content

etc are defined as object, not array while posting product data to Magento. Let me know the result.

 

-- Ravindra

Re: Consuming Magento REST service using Zend_OAuth_Consumer

Hi Ravindra,

 

I see the post that you comment me but the solution that the post say not is valid for me.

I want add the products use the zend oauth method and not the way the publication indicates that it is OAuth.

 

I have tried to add products into magento using SOAP and REST OAuth and the 2 ways function very well. But now I want use the zend OAuth method.

 

Best regards