{"id":21,"date":"2010-08-21T16:20:04","date_gmt":"2010-08-21T16:20:04","guid":{"rendered":"https:\/\/greladesign.co\/blog\/?p=21"},"modified":"2010-08-23T01:04:05","modified_gmt":"2010-08-23T00:04:05","slug":"flash-xml-rpc-magento","status":"publish","type":"post","link":"https:\/\/greladesign.co\/blog\/2010\/08\/21\/flash-xml-rpc-magento\/","title":{"rendered":"Flash->XML-RPC->Magento"},"content":{"rendered":"<p>In this article I&#8217;ll show you how to connect (login) and disconnect (release session) to the magento store using Magento API.<br \/>\n<!--more--><br \/>\n<em>I assume that you have working magento store somewhere (local or online).<\/em>First lets define the access to the store through the web services<\/p>\n<ul>\n<li> go to Admin area, System-&gt;Web Services-&gt;Roles (System-&gt;Us\u0142ugi Sieciowe-&gt;Role u\u017cytkownik\u00f3w)<\/li>\n<\/ul>\n<ul>\n<li>click on <strong>Add New Role<\/strong> (Dodaj now\u0105 rol\u0119)<\/li>\n<\/ul>\n<ul>\n<li>Type in &#8220;<strong>Role Name<\/strong>&#8221; (&#8220;Nazwa roli&#8221;) field desired name for new role then go to the Role Resources (Zasoby roli) tab in the left hand pane.<\/li>\n<\/ul>\n<ul>\n<li>Select all resources that you wish to be enabled for the user with this role (enabled means callable through web-service).<\/li>\n<\/ul>\n<p>I have got one &#8220;admin&#8221; role and I have applied all resources to it. Of course I can foresee that granular restriction would be more safe and in the end application I will set depending on the application and the suer itself, i.e.\u00a0 the sales guy doesn&#8217;t need to have access to, let say, &#8220;Catalog&#8221;.<\/p>\n<ul>\n<li>OK, once we have selected required resources, click on <strong>Save Role<\/strong> button.<\/li>\n<\/ul>\n<p>Create our remote user<\/p>\n<ul>\n<li> go to the System-&gt;Web Services-&gt;Users (System-&gt;Us\u0142ugi Sieciowe-&gt;U\u017cytkownicy)<\/li>\n<\/ul>\n<ul>\n<li>Click on <strong>Add New user<\/strong> (Dodaj nowego u\u017cytkownika)<\/li>\n<\/ul>\n<ul>\n<li>In the <strong>User Info<\/strong> (Info u\u017cytkownika) fill all required fields, they are pretty self explanatory except one:) Api Key &#8211; basically it is the password, you will use it along with User Name (in API documentation as Api User) to login to the magento.<\/li>\n<\/ul>\n<ul>\n<li>If you will do this go to the <strong>User Role<\/strong> tab in the left hand pane, and choose the role for this user (and through this what resources are available for this user). Once done click on <strong>Save User<\/strong> (Zapisz u\u017cytkownika).<\/li>\n<\/ul>\n<p>Now the real meat:) some code, this is the just proof-of-concept, quick and dirty.<\/p>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n\r\npackage\r\n{\r\n\timport flash.display.Sprite;\r\n\timport flash.events.Event;\r\n\timport flash.events.IOErrorEvent;\r\n\timport flash.events.ProgressEvent;\r\n\timport flash.events.SecurityErrorEvent;\r\n\timport flash.net.URLLoader;\r\n\timport flash.net.URLRequest;\r\n\timport flash.net.URLRequestMethod;\r\n\r\n\t\/**\r\n\t * Sample Code to show the login\/logout functionality of the Magento API\r\n\t * @author Lukasz 'Severiaan' Grela\r\n\t *\/\r\n\tpublic class Main extends Sprite\r\n\t{\r\n\t\tpublic static const LOGIN:String = &quot;login&quot;;\r\n\t\tpublic static const API_USER:String = &quot;YOUR_API_USER&quot;;\/\/i.e.RemoteUser\r\n\t\tpublic static const API_KEY:String = &quot;YOUR_API_KEY&quot;;\/\/i.e.RemoteUserPass\r\n\t\tpublic static const GATEWAY:String = &quot;YOUR_MAGENTO_STORE_URL\/api\/xmlrpc\/&quot;;\/\/i.e. http:\/\/127.0.0.1\/magento\/api\/xmlrpc\/\r\n\r\n\t\tpublic function Main():void\r\n\t\t{\r\n\t\t\tif (stage) init();\r\n\t\t\telse addEventListener(Event.ADDED_TO_STAGE, init);\r\n\t\t}\r\n\r\n\t\tprivate function init(e:Event = null):void\r\n\t\t{\r\n\t\t\tremoveEventListener(Event.ADDED_TO_STAGE, init);\r\n\t\t\t\/\/ entry point\r\n\t\t\t\/\/lets create our login request\r\n\t\t\tvar _xRequest:XML =&lt;methodCall&gt;\r\n\t\t\t\t\t\t\t\t  &lt;methodName&gt;{Main.LOGIN}&lt;\/methodName&gt;\r\n\t\t\t\t\t\t\t\t  &lt;params&gt;\r\n\t\t\t\t\t\t\t\t\t&lt;param&gt;\r\n\t\t\t\t\t\t\t\t\t  &lt;value&gt;{Main.API_USER}&lt;\/value&gt;\r\n\t\t\t\t\t\t\t\t\t&lt;\/param&gt;\r\n\t\t\t\t\t\t\t\t\t&lt;param&gt;\r\n\t\t\t\t\t\t\t\t\t  &lt;value&gt;{Main.API_KEY}&lt;\/value&gt;\r\n\t\t\t\t\t\t\t\t\t&lt;\/param&gt;\r\n\t\t\t\t\t\t\t\t  &lt;\/params&gt;\r\n\t\t\t\t\t\t\t\t&lt;\/methodCall&gt;;\r\n\r\n\t\t\t\/\/now we need to communicate with the webservice, to do this we need to use\r\n\t\t\tvar _oReq:URLRequest = new URLRequest(Main.GATEWAY);\r\n\t\t\t\t_oReq.contentType = &quot;text\/xml&quot;;\r\n\t\t\t\t_oReq.data = _xRequest;\r\n\t\t\t\ttrace(_oReq.data);\r\n\t\t\t\t_oReq.method = URLRequestMethod.POST;\r\n\r\n\t\t\tvar _oClient:URLLoader = new URLLoader();\r\n\r\n\t\t\t\t_oClient.addEventListener(ProgressEvent.PROGRESS, onProgress, false, 0, true);\r\n\t\t\t\t_oClient.addEventListener(IOErrorEvent.IO_ERROR, onIOError, false, 0, true);\r\n\t\t\t\t_oClient.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError, false, 0, true);\r\n\t\t\t\t_oClient.addEventListener(Event.COMPLETE, onResponse, false, 0, true);\r\n\r\n\t\t\t\t_oClient.load(_oReq);\r\n\r\n\t\t}\r\n\r\n\t\t\/**\r\n\t\t * Something went wrong and here we will tell about it to the user.\r\n\t\t * @param\terrorMsg\r\n\t\t *\/\r\n\t\tprotected function onFault(errorMsg:String):void\r\n\t\t{\r\n\t\t\ttrace(errorMsg);\r\n\t\t}\r\n\t\t\/**\r\n\t\t *\r\n\t\t * @param\tresponse\r\n\t\t *\/\r\n\t\tprotected function onSuccess(response:XML):void\r\n\t\t{\r\n\t\t\t\/\/this still doesn't meant that we have success, it merely indicate that URLLoader returned some values, if we are here it is XML formatted.\r\n\t\t\tif (response.fault.length() &gt; 0)\r\n\t\t\t{\r\n\t\t\t\tonFault(response.fault.value.toXMLString());\r\n\t\t\t}\r\n\t\t\telse\r\n\t\t\t{\r\n\t\t\t\tif (response.params.param.length() == 1)\r\n\t\t\t\t{\r\n\t\t\t\t\ttrace(response.params.param.value);\r\n\t\t\t\t}\r\n\t\t\t\telse\r\n\t\t\t\t{\r\n\t\t\t\t\t\/\/incorrect response format\r\n\t\t\t\t\tonFault(response.toXMLString());\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\t\/**\r\n\t\t * Event.COMPLETE handler, this is the first point to decide if we have success or failure after getting any response.\r\n\t\t * @param\te\r\n\t\t *\/\r\n\t\tprotected function onResponse(e:Event):void\r\n\t\t{\r\n\t\t\t\/\/wrapped with try as this might throw error when data returned is not a valid XML\r\n\t\t\ttry\r\n\t\t\t{\r\n\t\t\t\tonSuccess(new XML(e.target.data));\r\n\t\t\t}\r\n\t\t\tcatch (err:*)\r\n\t\t\t{\r\n\t\t\t\tonFault(&quot;Invalid response data\\n&quot;+e.target.data);\r\n\t\t\t}\r\n\t\t}\r\n\t\t\/**\r\n\t\t * SecurityErrorEvent.SECURITY_ERROR handler, this is the show stopper, it means that you can't access the remote resource (i.e. flash cannot load any data from different domain).\r\n\t\t * @param\te\r\n\t\t *\/\r\n\t\tprotected function onSecurityError(e:SecurityErrorEvent):void\r\n\t\t{\r\n\t\t\tonFault(&quot;Security Error: &quot;+e);\r\n\t\t}\r\n\t\t\/**\r\n\t\t * IOErrorEvent.IO_ERROR handler, this is the show stopper, it means that the remote resource is inaccessible, i.e. you have misspelled the URL, or it doesn't exist and so on:)\r\n\t\t * @param\te\r\n\t\t *\/\r\n\t\tprotected function onIOError(e:IOErrorEvent):void\r\n\t\t{\r\n\t\t\tonFault(&quot;IO Error: &quot;+e);\r\n\t\t}\r\n\t\t\/**\r\n\t\t * ProgressEvent.PROGRESS handler, you may show to the user the progress for the call (loader or something)\r\n\t\t * @param\te\r\n\t\t *\/\r\n\t\tprotected function onProgress(e:ProgressEvent):void\r\n\t\t{\r\n\t\t\t\/\/progress useful for large responses\r\n\t\t\ttrace(e);\r\n\t\t}\r\n\r\n\t}\r\n}\r\n\r\n<\/pre>\n<p>after running this code you should have similar result:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;methodCall&gt;\r\n  &lt;methodName&gt;login&lt;\/methodName&gt;\r\n  &lt;params&gt;\r\n    &lt;param&gt;\r\n      &lt;value&gt;RemoteUser&lt;\/value&gt;\r\n    &lt;\/param&gt;\r\n    &lt;param&gt;\r\n      &lt;value&gt;RemoteUserPass&lt;\/value&gt;\r\n    &lt;\/param&gt;\r\n  &lt;\/params&gt;\r\n&lt;\/methodCall&gt;\r\n[ProgressEvent type=&quot;progress&quot; bubbles=false cancelable=false eventPhase=2 bytesLoaded=169 bytesTotal=169]\r\n&lt;value&gt;\r\n  &lt;string&gt;35389ae00d8fc47b69e5c42e613fb25a&lt;\/string&gt;\r\n&lt;\/value&gt;\r\n\u00a0\r\n<\/pre>\n<p>OK, so it means that we have access granted, but also you may see something different, i.e.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n\r\n&lt;methodResponse&gt;\r\n  &lt;fault&gt;\r\n    &lt;value&gt;\r\n      &lt;struct&gt;\r\n        &lt;member&gt;\r\n          &lt;name&gt;faultCode&lt;\/name&gt;\r\n          &lt;value&gt;\r\n            &lt;int&gt;2&lt;\/int&gt;\r\n          &lt;\/value&gt;\r\n        &lt;\/member&gt;\r\n        &lt;member&gt;\r\n          &lt;name&gt;faultString&lt;\/name&gt;\r\n          &lt;value&gt;\r\n            &lt;string&gt;Access denied.&lt;\/string&gt;\r\n          &lt;\/value&gt;\r\n        &lt;\/member&gt;\r\n      &lt;\/struct&gt;\r\n    &lt;\/value&gt;\r\n  &lt;\/fault&gt;\r\n&lt;\/methodResponse&gt;\r\n\u00a0\r\n<\/pre>\n<p>So this is the fault response that you may see when your user doesn&#8217;t exist, password is not right. So let&#8217;s handle success now.<br \/>\nwe have one handler for success and another for failure, we need to make it more usable as at the moment we will not be able to say, for which command we received response (successful or not).<\/p>\n<p>Simple variable that will hold currently invoked command will do:<\/p>\n<pre class=\"brush: as3; first-line: 31; title: ; notranslate\" title=\"\">\r\n  protected var m_sCurrentCommand:String;\r\n<\/pre>\n<p>To make this example more usable I have modified further code from previous version:<\/p>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\npackage\r\n{\r\n\timport flash.display.Sprite;\r\n\timport flash.events.Event;\r\n\timport flash.events.IOErrorEvent;\r\n\timport flash.events.MouseEvent;\r\n\timport flash.events.ProgressEvent;\r\n\timport flash.events.SecurityErrorEvent;\r\n\timport flash.net.URLLoader;\r\n\timport flash.net.URLRequest;\r\n\timport flash.net.URLRequestMethod;\r\n\t\r\n\t\/**\r\n\t * Sample Code to show the login\/logout functionality of the Magento API\r\n\t * @author Lukasz 'Severiaan' Grela\r\n\t *\/\r\n\tpublic class Main extends Sprite\r\n\t{\r\n\t\tpublic static const LOGIN:String = &quot;login&quot;;\r\n\t\tpublic static const END_SESSION:String = &quot;endSession&quot;;\r\n\t\t\r\n\t\tpublic static const API_USER:String = &quot;YOUR_API_USER&quot;;\/\/i.e.RemoteUser\r\n\t\tpublic static const API_KEY:String = &quot;YOUR_API_KEY&quot;;\/\/i.e.RemoteUserPass\r\n\t\tpublic static const GATEWAY:String = &quot;YOUR_MAGENTO_STORE_URL\/api\/xmlrpc\/&quot;;\/\/i.e. http:\/\/127.0.0.1\/magento\/api\/xmlrpc\/\r\n\r\n\t\tprotected var m_sSessionId:String;\r\n\t\tprotected var m_oLogIn:CustomSimpleButton;\r\n\t\tprotected var m_oLogOut:CustomSimpleButton;\r\n\t\tprotected var _oReq:URLRequest;\r\n\t\tprotected var _oClient:URLLoader;\r\n\t\tprotected var m_sCurrentCommand:String;\r\n\t\t\r\n\t\t\r\n\t\tpublic function Main():void\r\n\t\t{\r\n\t\t\tif (stage) init();\r\n\t\t\telse addEventListener(Event.ADDED_TO_STAGE, init);\r\n\t\t}\r\n\t\t\r\n\t\tprivate function init(e:Event = null):void\r\n\t\t{\r\n\t\t\tremoveEventListener(Event.ADDED_TO_STAGE, init);\r\n\t\t\t\/\/ entry point\r\n\t\t\t\r\n\t\t\tm_oLogIn = new CustomSimpleButton(100, 22, 0x008000);\r\n\t\t\tm_oLogOut = new CustomSimpleButton(100, 22, 0xFA0000);\r\n\t\t\t\r\n\r\n\t\t\t\/\/now we need to communicate with the webservice, to do this we need to use\r\n\t\t\t_oReq = new URLRequest(Main.GATEWAY);\r\n\t\t\t\t_oReq.contentType = &quot;text\/xml&quot;;\r\n\t\t\t\t_oReq.method = URLRequestMethod.POST;\r\n\t\t\t\t\r\n\t\t\t_oClient = new URLLoader();\r\n\t\t\t\r\n\t\t\t\t_oClient.addEventListener(ProgressEvent.PROGRESS, onProgress, false, 0, true);\r\n\t\t\t\t_oClient.addEventListener(IOErrorEvent.IO_ERROR, onIOError, false, 0, true);\r\n\t\t\t\t_oClient.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError, false, 0, true);\r\n\t\t\t\t_oClient.addEventListener(Event.COMPLETE, onResponse, false, 0, true);\r\n\t\t\t\r\n\t\t\t\t\r\n\t\t\taddChild(m_oLogIn);\r\n\t\t\taddEventListener(MouseEvent.CLICK, onClick, false, 0, true);\r\n\t\t\t\t\/\/\r\n\t\t\t\r\n\t\t}\r\n\t\t\/**\r\n\t\t * Log in\r\n\t\t *\/\r\n\t\tprotected function login():void\r\n\t\t{\r\n\t\t\tm_sCurrentCommand = Main.LOGIN;\r\n\t\t\t\/\/lets create our login request\r\n\t\t\tvar _xRequest:XML = &lt;methodCall&gt;\r\n\t\t\t\t\t\t\t\t  &lt;methodName&gt;{m_sCurrentCommand}&lt;\/methodName&gt;\r\n\t\t\t\t\t\t\t\t  &lt;params&gt;\r\n\t\t\t\t\t\t\t\t\t&lt;param&gt;\r\n\t\t\t\t\t\t\t\t\t  &lt;value&gt;{Main.API_USER}&lt;\/value&gt;\r\n\t\t\t\t\t\t\t\t\t&lt;\/param&gt;\r\n\t\t\t\t\t\t\t\t\t&lt;param&gt;\r\n\t\t\t\t\t\t\t\t\t  &lt;value&gt;{Main.API_KEY}&lt;\/value&gt;\r\n\t\t\t\t\t\t\t\t\t&lt;\/param&gt;\r\n\t\t\t\t\t\t\t\t  &lt;\/params&gt;\r\n\t\t\t\t\t\t\t\t&lt;\/methodCall&gt;;\r\n\t\t\t\/\/\r\n\t\t\t_oReq.data = _xRequest;\r\n\t\t\t\/\/\r\n\t\t\ttrace(&quot;login()&quot;);\r\n\t\t\ttrace(_oReq.data);\r\n\t\t\t\/\/\r\n\t\t\t_oClient.load(_oReq);\r\n\t\t\t\r\n\t\t}\r\n\t\t\/**\r\n\t\t * Log out\r\n\t\t *\/\r\n\t\tprotected function logout():void\r\n\t\t{\r\n\t\t\t\/\/we need to create another request, this time we will use endSession method\r\n\t\t\tm_sCurrentCommand = Main.END_SESSION;\r\n\t\t\t\/\/one note, so far we have covered only string data type in XML-RPC, this is the only one type that is allowed to skip it's type node, i.e. &lt;string&gt;abc&lt;\/string&gt; is equal to abc alone.\r\n\t\t\tvar _xRequest:XML = &lt;methodCall&gt;\r\n\t\t\t\t\t\t\t\t  &lt;methodName&gt;{m_sCurrentCommand}&lt;\/methodName&gt;\r\n\t\t\t\t\t\t\t\t  &lt;params&gt;\r\n\t\t\t\t\t\t\t\t\t&lt;param&gt;\r\n\t\t\t\t\t\t\t\t\t  &lt;value&gt;{m_sSessionId}&lt;\/value&gt;\r\n\t\t\t\t\t\t\t\t\t&lt;\/param&gt;\r\n\t\t\t\t\t\t\t\t  &lt;\/params&gt;\r\n\t\t\t\t\t\t\t\t&lt;\/methodCall&gt;;\r\n\t\t\t\/\/\r\n\t\t\t_oReq.data = _xRequest;\r\n\t\t\t\/\/\r\n\t\t\ttrace(&quot;logout()&quot;);\r\n\t\t\ttrace(_oReq.data);\r\n\t\t\t\/\/\r\n\t\t\t_oClient.load(_oReq);\r\n\t\t\t\t\/\/hide button to dissallow multiple click:-)\r\n\t\t\t\tif (contains(m_oLogOut)) removeChild(m_oLogOut);\r\n\t\t}\r\n\t\t\r\n\t\t\/**\r\n\t\t * Something went wrong and here we will tell about it to the user.\r\n\t\t * @param\terrorMsg\r\n\t\t *\/\r\n\t\tprotected function onFault(errorMsg:String):void\r\n\t\t{\r\n\t\t\tswitch (m_sCurrentCommand)\r\n\t\t\t{\r\n\t\t\t\tcase Main.LOGIN:\r\n\t\t\t\t\t\/\/handle login error, i.e. show login panel again\r\n\t\t\t\tbreak;\r\n\t\t\t\tcase Main.END_SESSION:\r\n\t\t\t\t\t\/\/handle end session error, i.e. do nothing:) as it possibly was caused by session expired, or trying to end alread ended session\r\n\t\t\t\tbreak;\r\n\t\t\t\tdefault:\r\n\t\t\t}\r\n\t\t\ttrace(&quot;Call to &quot; + m_sCurrentCommand + &quot; was unsuccessfull.&quot;);\r\n\t\t\ttrace(errorMsg);\r\n\t\t}\r\n\t\t\/**\r\n\t\t * we have received an xml in response\r\n\t\t * @param\tresponse\r\n\t\t *\/\r\n\t\tprotected function onSuccess(response:XML):void\r\n\t\t{\r\n\t\t\ttrace(response);\r\n\t\t\t\/\/this still doesn't meant that we have success, it merely indicate that URLLoader returned some values, if we are here it is XML formatted.\r\n\t\t\tif (response.fault.length() &gt; 0)\r\n\t\t\t{\r\n\t\t\t\tonFault(response.fault.value.toXMLString());\r\n\t\t\t}\r\n\t\t\telse\r\n\t\t\t{\r\n\t\t\t\tif (response.params.param.length() == 1)\r\n\t\t\t\t{\r\n\t\t\t\t\tswitch (m_sCurrentCommand)\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tcase Main.LOGIN:\r\n\t\t\t\t\t\t\t\/\/ok so we have success\r\n\t\t\t\t\t\t\t\/\/response to the login command is the sessionid of type string, we can access it here like this, we need to store it as it will be used with any other calls.\r\n\t\t\t\t\t\t\tm_sSessionId = response.params.param.value.string;\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\ttrace(&quot;login returned:&quot; + (response.params.param.value.string));\r\n\t\t\t\t\t\t\t\/\/show logout button\/\/green\r\n\t\t\t\t\t\t\taddChild(m_oLogOut);\r\n\t\t\t\t\t\t\t\/\/remove login\/\/red\r\n\t\t\t\t\t\t\tif (contains(m_oLogIn)) removeChild(m_oLogIn);\r\n\t\t\t\t\t\tbreak;\r\n\t\t\t\t\t\tcase Main.END_SESSION:\r\n\t\t\t\t\t\t\t\/\/endSession returns Boolean value\r\n\t\t\t\t\t\t\ttrace(&quot;endSession returned:&quot; + (response.params.param.value.boolean));\r\n\t\t\t\t\t\t\t\/\/\r\n\t\t\t\t\t\t\taddChild(m_oLogIn);\r\n\t\t\t\t\t\t\t\/\/\r\n\t\t\t\t\t\t\tif (contains(m_oLogOut)) removeChild(m_oLogOut);\r\n\t\t\t\t\t\tbreak;\r\n\t\t\t\t\t\tdefault:\r\n\t\t\t\t\t}\r\n\t\t\t\t\t\/\/clear command\r\n\t\t\t\t\tm_sCurrentCommand = &quot;&quot;;\r\n\t\t\t\t}\r\n\t\t\t\telse\r\n\t\t\t\t{\r\n\t\t\t\t\t\/\/incorrect response format\r\n\t\t\t\t\tonFault(response.toXMLString());\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t\t\r\n\t\t\r\n\t\t\/**\r\n\t\t * MouseEvent.CLICK handler\r\n\t\t * @param\te\r\n\t\t *\/\r\n\t\tprotected function onClick(e:MouseEvent):void\r\n\t\t{\r\n\t\t\tswitch (e.target)\r\n\t\t\t{\r\n\t\t\t\tcase m_oLogIn:\r\n\t\t\t\t\tlogin();\r\n\t\t\t\tbreak;\r\n\t\t\t\tcase m_oLogOut:\r\n\t\t\t\t\tlogout();\r\n\t\t\t\tbreak;\r\n\t\t\t}\r\n\t\t}\r\n\t\t\r\n\t\t\r\n\t\t\/**\r\n\t\t * Event.COMPLETE handler, this is the first point to decide if we have success or failure after getting any response.\r\n\t\t * @param\te\r\n\t\t *\/\r\n\t\tprotected function onResponse(e:Event):void\r\n\t\t{\r\n\t\t\t\/\/wrapped with try as this might throw error when data returned is nto a valid XML\r\n\t\t\ttry\r\n\t\t\t{\r\n\t\t\t\tonSuccess(new XML(e.target.data));\r\n\t\t\t}\r\n\t\t\tcatch (err:*)\r\n\t\t\t{\r\n\t\t\t\tonFault(&quot;Invalid response data\\n&quot;+e.target.data);\r\n\t\t\t}\r\n\t\t}\r\n\t\t\/**\r\n\t\t * SecurityErrorEvent.SECURITY_ERROR handler, this is the showstopper, it means that you can't access the remote resource (i.e. flash cannot load any data from different domain).\r\n\t\t * @param\te\r\n\t\t *\/\r\n\t\tprotected function onSecurityError(e:SecurityErrorEvent):void\r\n\t\t{\r\n\t\t\tonFault(&quot;Security Error: &quot;+e);\r\n\t\t}\r\n\t\t\/**\r\n\t\t * IOErrorEvent.IO_ERROR handler, this is the showstopper, it meanst that the remote resource is inaccessible, i.e. you have misspelled the URL, or it doesn't exist and so on:)\r\n\t\t * @param\te\r\n\t\t *\/\r\n\t\tprotected function onIOError(e:IOErrorEvent):void\r\n\t\t{\r\n\t\t\tonFault(&quot;IO Error: &quot;+e);\r\n\t\t}\r\n\t\t\/**\r\n\t\t * ProgressEvent.PROGRESS handler, you may show to the user the progress fo the call (loader or something)\r\n\t\t * @param\te\r\n\t\t *\/\r\n\t\tprotected function onProgress(e:ProgressEvent):void\r\n\t\t{\r\n\t\t\t\/\/progress usefull for large responses\r\n\t\t\ttrace(e);\r\n\t\t}\r\n\t\t\r\n\t}\r\n}\r\nimport flash.display.DisplayObject;\r\nimport flash.display.Shape;\r\nimport flash.display.SimpleButton;\r\n\r\nclass CustomSimpleButton extends SimpleButton\r\n{\r\n    private var upColor:Number   = 0xFFCC00;\r\n    private var overColor:Number = 0xCCFF00;\r\n    private var downColor:Number = 0x00CCFF;\r\n\r\n    public function CustomSimpleButton(width:Number, height:Number, baseColor:Number)\r\n\t{\r\n\t\tupColor = baseColor;\r\n        downState      = new ButtonDisplayState(downColor, width, height);\r\n        overState      = new ButtonDisplayState(overColor, width, height);\r\n        upState        = new ButtonDisplayState(upColor, width, height);\r\n        hitTestState   = new ButtonDisplayState(upColor, width, height);\r\n        hitTestState.x = 0;\r\n        hitTestState.y = 0;\r\n\t\t\r\n        useHandCursor  = true;\r\n    }\r\n}\r\n\r\nclass ButtonDisplayState extends Shape\r\n{\r\n    private var bgColor:Number;\r\n    private var _width:uint;\r\n    private var _height:uint;\r\n\r\n    public function ButtonDisplayState(bgColor:Number, width:Number, height:Number)\r\n\t{\r\n        this.bgColor = bgColor;\r\n        _width = width;\r\n        _height = height;\r\n        draw();\r\n    }\r\n\r\n    private function draw():void\r\n\t{\r\n        graphics.beginFill(bgColor);\r\n        graphics.drawRect(0, 0, _width, _height);\r\n        graphics.endFill();\r\n    }\r\n}\r\n\u00a0\r\n<\/pre>\n<p>This code will generate following output (on success:)<\/p>\n<p>Click on log in button (green)<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n\r\nlogin()\r\n&lt;methodCall&gt;\r\n  &lt;methodName&gt;login&lt;\/methodName&gt;\r\n  &lt;params&gt;\r\n    &lt;param&gt;\r\n      &lt;value&gt;remoteUser&lt;\/value&gt;\r\n    &lt;\/param&gt;\r\n    &lt;param&gt;\r\n      &lt;value&gt;remoteUserPass&lt;\/value&gt;\r\n    &lt;\/param&gt;\r\n  &lt;\/params&gt;\r\n&lt;\/methodCall&gt;\r\n\u00a0\r\n<\/pre>\n<p>receving response<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n\r\n[ProgressEvent type=&quot;progress&quot; bubbles=false cancelable=false eventPhase=2 bytesLoaded=169 bytesTotal=169]\r\n&lt;methodResponse&gt;\r\n  &lt;params&gt;\r\n    &lt;param&gt;\r\n      &lt;value&gt;\r\n        &lt;string&gt;95d69fef8ca3257da28d6d5bdbbe3b1d&lt;\/string&gt;\r\n      &lt;\/value&gt;\r\n    &lt;\/param&gt;\r\n  &lt;\/params&gt;\r\n&lt;\/methodResponse&gt;\r\nlogin returned:95d69fef8ca3257da28d6d5bdbbe3b1d\r\n\u00a0\r\n<\/pre>\n<p>clicking on log out button (red)<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n\r\nlogout()\r\n&lt;methodCall&gt;\r\n  &lt;methodName&gt;endSession&lt;\/methodName&gt;\r\n  &lt;params&gt;\r\n    &lt;param&gt;\r\n      &lt;value&gt;95d69fef8ca3257da28d6d5bdbbe3b1d&lt;\/value&gt;\r\n    &lt;\/param&gt;\r\n  &lt;\/params&gt;\r\n&lt;\/methodCall&gt;\r\n\u00a0\r\n<\/pre>\n<p>receiving response<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n\r\n[ProgressEvent type=&quot;progress&quot; bubbles=false cancelable=false eventPhase=2 bytesLoaded=140 bytesTotal=140]\r\n&lt;methodResponse&gt;\r\n  &lt;params&gt;\r\n    &lt;param&gt;\r\n      &lt;value&gt;\r\n        &lt;boolean&gt;1&lt;\/boolean&gt;\r\n      &lt;\/value&gt;\r\n    &lt;\/param&gt;\r\n  &lt;\/params&gt;\r\n&lt;\/methodResponse&gt;\r\nendSession returned:1\r\n\u00a0\r\n<\/pre>\n<p>That is all for now, happy coding:)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article I&#8217;ll show you how to connect (login) and disconnect (release session) to the magento store using Magento API.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":""},"categories":[9,4,8],"tags":[12,301,11,13,5],"_links":{"self":[{"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/posts\/21"}],"collection":[{"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/comments?post=21"}],"version-history":[{"count":41,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/posts\/21\/revisions"}],"predecessor-version":[{"id":53,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/posts\/21\/revisions\/53"}],"wp:attachment":[{"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/media?parent=21"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/categories?post=21"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/tags?post=21"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}