Skip to content

Magento Rest Client

Ralph Soika edited this page Aug 30, 2014 · 3 revisions

Magento Rest Client

The MagentoRestClient is used to connect to the Magento Rest API. This is one of the two Web Services provided by Magento. For authentication against the Magento Rest API, Magento uses the OAuth 1.0.a protocol. See the Magento API documentation for further information.

The MagentoRestClient can be configured by the imixs.property file or the imixs PropertyService . The following example shows a typical configuration of the imixs.properties including the OAuht access keys:

##############################
# Magento REST Service Configuration 
##############################
# magento host
magento.uri-basis=http://localhost/magento/index.php/
magento.uri-api=http://localhost/magento/api/rest

# OAuth consumer token
magento.token.consumer-key=7ec2d5c3ef2ad29d.......
magento.token.consumer-secret=19002a261f4b4........

# OAuth access token... 
magento.token.access-key=m0scpjq3wsje9......
magento.token.access-secret=e05b4m4p8b9n....

# optional 
magento.debug=true

Generating an Access-Token

To access the Magento Rest API it is necessary to configure the OAuth based access module from Magento. First you need to create a consumer token in Magento Admin Interface to be used by your Workflow application. With the consumer token you can generate an access token. Both tokens (consumer and access) are necessary to authenticate a third party application like Imixs-Workflow.

The MagentoRestClient provides a set of methods to generate a valid OAuth access token. For easy usage the junit Test class 'org.imixs.workflow.magento.rest.test.TestMagentoPlugin' can be used to generate a valid access token by calling the test method 'testRequestNewToken()'. If you run this test the MagentoRestClient requests a new access token in an interactive way. Before you start the test method make sure hat the properties 'magento.uri-basis', 'magento.token.consumer-key' and 'magento.token.consumer-secret' are configured by the imixs.properties file!.

Running the test will print a request URL to call from a web Browser. Then you can login into Magento and gain the access. The verifier need to be entered into the live console. After that a new access token (key and secret) will be generated. See the following example of a test run:

Open Browser Window and authorize the Imixs MagentoPlugin here:
http://localhost/magento/index.php/admin/oauth_authorize?oauth_token=261a3514776fbaeef56xxxxxxxx
And paste the verifier here
>>949775df82a747d09b72exxxxxxxxxx
Got the Access Token!
   key=7ec2d5c3ef2ad29d5519773dd0537a44
   secret=19002a261f4b42a0d4b86388757fa2b4

To run further tests the key and secret generated by the JUnit test need to be entered into the properties 'magento.token.access-key' and 'magento.token.access-secrete' of imixs.porperties file.

The MagentoRestClient implements an Interface which provides several methods to access the Magento Rest API. The Client is also used by the MagentoPlugin class. The Client initialize the OAuth Data to access the Magento Web Service Rest API. There for the Client uses the Scribe library. The configuration can be stored in a config Entity 'ConfigMagento' or in the imixs.properties file. The configuration is read during the @PostConstruct call back method. The method getService() returns a OAuthService object which can be used to interact in a flexible way with the OAuth Service.