使用SOAP API的android magento客户登录(身份验证)

时间:2014-07-31 09:44:22

标签: android magento soap

我在尝试登录(认证)magento客户时遇到问题。我无法找到使用电子邮件和密码登录客户的正确方法。那么,你能告诉我如何使用SOAP API在magento商店中进行身份验证或登录客户。        我尝试了这个代码,如下所示

env.dotNet = false;
            env.xsd = SoapSerializationEnvelope.XSD;
            env.enc = SoapSerializationEnvelope.ENC;

            SoapObject request = new SoapObject(NAMESPACE, "login");

            request.addProperty("username", "xyz");
            request.addProperty("apiKey", "xyz");

            env.setOutputSoapObject(request);

            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            androidHttpTransport.debug = true;
            androidHttpTransport.call("", env);
            Object result = env.getResponse();

            Log.d("sessionId", result.toString());

            // making call to get list of customers

            String sessionId = result.toString();
            request = new SoapObject(NAMESPACE, "customerCustomerInfo");
            request.addProperty("sessionId", sessionId);
            request.addProperty("customerId", "2032");
            request.addProperty("email", "abc@def.com");
            request.addProperty("password", "password");


            env.setOutputSoapObject(request);
            androidHttpTransport.call(SOAP_ACTION, env);
            androidHttpTransport.debug = true;
            result = env.getResponse();

            Log.d("Customer List", result.toString());

}

但它对我没有帮助,所以有没有人能解决我的问题。

谢谢....

4 个答案:

答案 0 :(得分:6)

没有直接选择将客户登录到magento。从API检索的密码是hash_password,您无法检查密码是否相等。但您可以使用下面介绍的方法登录magento。

  1. 创建外部php文件并访问magento登录

    require_once( '../ magentosite /应用/ Mage.php'); // Magento的路径 的umask(0); 法师::应用程式();

    $ id = 1;

    尝试{     $ result = Mage :: getModel('customer / customer') - > setWebsiteId($ id) - > authenticate($ email,$ password); } catch(Exception $ e){     $ result = false; }

  2. 使用JSON将用户名和密码从android发送到该php页面,并获得“结果”。

  3. 如果结果为“true”,则DB中存在用户名和密码。

答案 1 :(得分:3)

实际上你可以使用SOAP检查/登录用户,你只需要稍微扩展一下。在app / code / core / Mage / Customer / Model / Customer / Api.php添加新功能

public function login($email, $password){
        /** @var $session Mage_Customer_Model_Session */
        $session = Mage::getSingleton( 'customer/session' );
        Mage::app()->getStore()->setWebsiteId(1);
        try
        {
            $session->login( $email, $password );
            $customer = $session->getCustomer();

            return json_encode(array('status' => 'OK', 'userData' => $this->info($customer->getId())));
        }
        catch( Exception $e )
        {
            return json_encode(array('status' => 'error', 'message' => $e->getMessage()));
        }
    }

在app / code / core / Mage / Customer / etc / api.xml

<config>
<api>
    <resources>
        <customer translate="title" module="customer">
         <methods>
        ...
               <login translate="title" module="customer">
                    <title>Login customer</title>
                    <acl>customer/login</acl>
                </login>

也在最后

        <acl>
        <resources>
            <customer translate="title" module="customer">
            ...
            <login translate="title" module="customer">
                    <title>Login</title>
            </login>

在这里你可以测试你的登录功能

<?php
$host = "http://youmagentohost/index.php";
$client = new SoapClient($host."/api/soap/?wsdl"); //soap handle
$apiuser= "apiuser"; //webservice user login
$apikey = "apikey"; //webservice user pass
$action = "customer.login";
try {

    $sess_id= $client->login($apiuser, $apikey);
    $params = array('email'=>'email@email.com', 'password'=>'password');

    print_r($client->call($sess_id, $action, $params));
}
catch (Exception $e) { //while an error has occured
    echo "==> Error: ".$e->getMessage();
    exit();
}
?>

它不是很好的解决方案,但总比没有好。不要忘记重写所有核心文件;)

答案 2 :(得分:0)

实际上你可以使用SOAP检查/登录用户 在app / code / core / local / Envato / Masterapi / etc / api.xml

<?xml version="1.0"?>
<config>
  <api>

    <resources>

      <masterapi_loginmodel translate="title" module="masterapi">
        <model>masterapi/loginmodel_api</model>
        <title>Demo Custommoduleapi API</title>
        <acl>masterapi/loginmodel</acl>
        <methods>
         <!-- <list translate="title" module="masterapi">
            <title>List of masterapi</title>
            <method>clogin</method>
          </list> -->
          <customerlogin translate="title" module="masterapi">
           <title>List of masterapi</title>
          </customerlogin>

        </methods>
      </masterapi_loginmodel>




    </resources>


    <resources_alias>
      <loginmodel>masterapi_loginmodel</loginmodel>

    </resources_alias>

    <v2>
      <resources_function_prefix>
         <loginmodel>masterapiLoginmodel</loginmodel>

      </resources_function_prefix>
    </v2>

    <acl>
      <resources>
        <masterapi translate="title" module="masterapi">
          <title>Loginmodel</title>
          <sort_order>5</sort_order>
          <loginmodel translate="title" module="masterapi">
            <title>loginmodel data</title>
          </loginmodel>


        </masterapi>

      </resources>
    </acl>
  </api>
</config>

在app / code / core / local / Envato / Masterapi / etc / config.xml

 <?xml version="1.0"?>
    <config>
      <modules>
        <Mycustom_Masterapi>
          <version>1.0</version>
        </Mycustom_Masterapi>
      </modules>
      <global>
        <models>
          <masterapi>
            <class>Mycustom_Masterapi_Model</class>
          </masterapi>
        </models>
        <helpers>
          <masterapi>
            <class>Mycustom_Masterapi_Helper</class>
          </masterapi>
        </helpers>
      </global>
    </config>

在app / code / core / local / Envato / Masterapi / etc / wsdl.xml

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns:typens="urn:{{var wsdl.name}}" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
  xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/"
  name="{{var wsdl.name}}" targetNamespace="urn:{{var wsdl.name}}">

  <types>
    <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:Magento">
      <import namespace="http://schemas.xmlsoap.org/soap/encoding/" schemaLocation="http://schemas.xmlsoap.org/soap/encoding/" />
      <complexType name="fieldInfo">
        <sequence>
          <element name="entity_id" type="xsd:string"/>
          <element name="name" type="xsd:string"/>
        </sequence>
      </complexType>
      <complexType name="fieldInfoArray">
        <complexContent>
          <restriction base="soapenc:Array">
            <attribute ref="soapenc:arrayType" wsdl:arrayType="typens:fieldInfo[]" />
          </restriction>
        </complexContent>
      </complexType>




    </schema>
  </types>

  <message name="masterapiLoginmodelListRequest">
    <part name="sessionId" type="xsd:string" />
    <part name="user" type="xsd:string" />
     <part name="pass" type="xsd:string" /> 
  </message>

  <message name="masterapiLoginmodelListResponse">
    <part name="customlogin" type="xsd:string" />
  </message>



  <portType name="{{var wsdl.handler}}PortType">
    <operation name="masterapiLoginmodelCustomerlogin">
      <documentation>List of masterapi</documentation>
      <input message="typens:masterapiLoginmodelListRequest" />
      <output message="typens:masterapiLoginmodelListResponse" />
    </operation>



  </portType>





  <binding name="{{var wsdl.handler}}Binding" type="typens:{{var wsdl.handler}}PortType">
    <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
    <operation name="masterapiLoginmodelCustomerlogin">
      <soap:operation soapAction="urn:{{var wsdl.handler}}Action" />
      <input>
        <soap:body namespace="urn:{{var wsdl.name}}" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
      </input>
      <output>
        <soap:body namespace="urn:{{var wsdl.name}}" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
      </output>
    </operation>



  </binding>



  <service name="{{var wsdl.name}}Service">
    <port name="{{var wsdl.handler}}Port" binding="typens:{{var wsdl.handler}}Binding">
      <soap:address location="{{var wsdl.url}}" />
    </port>
  </service>
</definitions>

在app / code / local / Mycustom / Masterapi / Model / Loginmodel / Api.php

<?php
// app/code/local/Mycustom/Masterapi/Model/Loginmodel/Api.php
class Mycustom_Masterapi_Model_Loginmodel_Api 
{
  public function customerlogin($user,$pass)
  {

  /** @var $session Mage_Customer_Model_Session */
        $session = Mage::getSingleton( 'customer/session' );
        Mage::app()->getStore()->setWebsiteId(1);
        try
        {
            $session->login( $user, $pass);
            $customer = $session->getCustomer();

            return  json_encode(array('status' => 'valid', 'userData' => $customer->getId()));
        }
        catch( Exception $e )
        {
            return  json_encode(array('status' => 'invalid', 'userData' => $e->getMessage()));
        }



  }
}

在/app/code/local/Mycustom/Masterapi/Model/Loginmodel/Api/V2.php

<?php
//app/code/local/Envato/Customapimodule/Model/Product/Api/V2.php
class Mycustom_Masterapi_Model_Loginmodel_Api_V2 extends Mycustom_Masterapi_Model_Loginmodel_Api
{
}

在android方面: 呼叫肥皂请求:

      properties.put("sessionId", sessionId);
                properties.put("user", "qbc@fgfg.com"); 
                properties.put("pass","pass");
           request = new SoapObject(NAMESPACE, "masterapiLoginmodelCustomerlogin");
 env.setOutputSoapObject(request);
        androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.call("", env);

答案 3 :(得分:0)

以客户身份登录的最简单方法是:

  1. 获取所有客户的电子邮件ID
  2. 将其与输入的电子邮件ID匹配
  3. 如果匹配,则获取哈希密码
  4. 使用&#34;:&#34;
  5. 拆分哈希密码
  6. 使用md5方法加密输入的密码。 MD5方法需要字符串,一个是&#34; salt&#34;(在&#34;:&#34;之后是哈希密码中的字符串)和其他输入的密码。
  7. 匹配MD5的输出和哈希密码(&#34;之前的值;&#34;)
  8. 以下是代码:

    email = "xyz.@gmail.com";
    string_password = "xyz@123";
    request = new SoapObject(NAMESPACE, "customerCustomerList");
    request.addProperty("sessionId",sessionId );            
    env.setOutputSoapObject(request);
    androidHttpTransport.call("urn:Magento/customerCustomerList", env);
    SoapObject response =  (SoapObject) env.getResponse();      
    
    for(int i=1;i<lengthofResponse;i++)
    {                         
    
       SoapObject  CustomerList = (SoapObject)response.getProperty(i);                        
       Object email_id=CustomerList.getProperty(6);
       int CustomerList_length = CustomerList.getPropertyCount();
       Object password=CustomerList.getProperty((CustomerList_length));
       selected_email_ids.add(email_id.toString());
       response_password=(password.toString().split(":"));
       selected_password.add(response_password[0]);     
       selected_salt.add(response_password[1]);     
       //comparing password                  
       if(email.equals(selected_email_ids.get(j)))
       {
            System.out.println("Email is currect");
            hash = md5( response_password[1] + string_password);
            if(hash.equals(selected_password.get(j)))
            {
                System.out.println("Welcome");
            }
        }                                                   
    }