Friday, August 7, 2015

Authentication and Cart Functionality in Hybris via Webservices

Imagine a scenario when you want a third party application to be in sync with your Hybris application (this may also be a mobile app).

Products ordered in one application should also be visible in the second application.

This scenario can be easily achieved by using webservices which are provided by Hybris OOTB (Out-Of-The-Box).


Steps->

1) If we login to the webportal for the first time, we can see that the cart is empty.



2)      Now logout from the portal.

3)      Now we can add products to the cart by using the below web-services.

4)      //webservice for authentication
http://localhost:9001/rest/oauth/token?client_id=mobile_android&client_secret=secret&grant_type=password&username=hrishikesh.maluskar@igate.com&password=Aug@2015

After you run the above webservice, you get the following output ->
{
  "access_token": "7bd2b9a2-24d6-4939-9c8f-d98b4e464b6a",
  "token_type": "bearer",
  "refresh_token": "efaf6ed2-746e-4263-93dd-f37e485ded7f",
  "expires_in": 17571
}

You need to add this token in the header of RestClient as shown below.


5)      //get the current logged in user
https://localhost:9002/rest/v1/electronics/customers/current

6)      //get the current session cart
http://localhost:9001/rest/v1/electronics/cart

7)      //add to cart
http://localhost:9001/rest/v1/electronics/cart/entry?code=107701&qty=2

8)      //logout current user
http://localhost:9001/rest/v1/electronics/customers/current/logout

9)      Now if you go to the webstore, you will see the product gets added to the cart as shown below.





 

Note: You can also add the header in your JAVA code as shown below.

WebResource webResource = client.resource(your-webservice);
webResource.header("Authorization", "bearer 7bd2b9a2-24d6-4939-9c8f-d98b4e464b6a")



No comments:

Post a Comment

Home