cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2 C# APIs SOAP problem

SOLVED

Magento 2 C# APIs SOAP problem

I have to develop an C# application which shoud integrate Magento 2 with an ERP. I want to use Magento 2 web services API's, with SOAP authentication.

When I try to add some web references to my Project like this to access Product Services:

http://[magento-ip]/soap/default?wsdl=1&services=catalogProductAttributeGroupRepositoryV1,catalogPro...

I get an XML file with the message:

"Consumer is not authorized to access %resources"

I have also installed Wizdler extension to Google Chrome to parse XML files and get the same error file trying to access the methods.

I can access to Login/Token Services and get a Token session, but I can't get access to Product APIs without adding web reference in my Project. I can also access to other Services like Customer -Services.

It was relatively easy to access Magento web services and API's access with Magento 1.7 because you only have to specify your token with parameters in your methods, but in Magento 2 it's quite different. I also can't get documentation about Magento 2 API's.

Does anyone know why this happens? Any help would be appreciated.

[For your information, I am using Visual Studio 2010. Could be this the problem??]

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Magento 2 C# APIs SOAP problem

I read something about putting a loopback into the server for the server, something your provider will need to do.

 

edit: also edited out some of the new 'security' that breaks Visual Studio WSDL creation tool.

 

Edit file: vendor / magento / module-webapi / Model / AbstractSchemaGenerator.php

 

 public function getListOfServices()
    {
        $listOfAllowedServices = [];
        foreach ($this->serviceMetadata->getServicesConfig() as $serviceName => $service) {
            foreach ($service[ServiceMetadata::KEY_SERVICE_METHODS] as $method)
            {
             //  if ($this->authorization->isAllowed($method[ServiceMetadata::KEY_ACL_RESOURCES]))
             //        {
                    $listOfAllowedServices[] = $serviceName;
                    break;
             //   }
            }
        }
        return $listOfAllowedServices;
    }

 

/***********aaaaaaaaaaand***/

 

protected function getAllowedServicesMetadata($requestedServices)
    {
        $allowedServicesMetadata = [];
        foreach ($requestedServices as $serviceName) {
            $serviceMetadata = $this->getServiceMetadata($serviceName);
            foreach ($serviceMetadata[ServiceMetadata::KEY_SERVICE_METHODS] as $methodName => $methodData) {
                if (!$this->authorization->isAllowed($methodData[ServiceMetadata::KEY_ACL_RESOURCES]))
                {
                   // unset($serviceMetadata[ServiceMetadata::KEY_SERVICE_METHODS][$methodName]);
                }
            }
            if (!empty($serviceMetadata[ServiceMetadata::KEY_SERVICE_METHODS]))
            {
                $this->removeRestrictedRoutes($serviceMetadata);
                $allowedServicesMetadata[$serviceName] = $serviceMetadata;
            }
        }
        return $allowedServicesMetadata;
    }

    /**
     * Remove routes which should not be visible to current user.
     *
     * @param array &$serviceMetadata
     * @return void
     */
    protected function removeRestrictedRoutes(&$serviceMetadata)
    {
        $allowedMethodNames = array_keys($serviceMetadata[ServiceMetadata::KEY_SERVICE_METHODS]);
        /** Remove routes which reference methods not visible to current user */
        if (isset($serviceMetadata[ServiceMetadata::KEY_ROUTES])) {
            foreach ($serviceMetadata[ServiceMetadata::KEY_ROUTES] as $path => &$routeGroup) {
                foreach ($routeGroup as $httpMethod => &$route) {
                    if (!in_array($route[ServiceMetadata::KEY_ROUTE_METHOD], $allowedMethodNames)) {
                   //     unset($routeGroup[$httpMethod]);
                    }
                }
                if (empty($routeGroup)) {
                 // unset($serviceMetadata[ServiceMetadata::KEY_ROUTES][$path]);
                }
            }
        }
    }

 

View solution in original post

6 REPLIES 6

Re: Magento 2 C# APIs SOAP problem

Hi,

 

It looks like Mage 2 has moved the simple SOAP WSDL generation URL from [magesite] /index.php/api/v2_soap?wsdl=1 and unhelpfully broke it down into chunks.

 

I *think* you need to access the following URL format to generate the code:

 

[magentosite]/soap?wsdl&services=customerV1

 

more info here

 

http://devdocs.magento.com/guides/v2.0/get-started/soap/soap-web-api-calls.html

Re: Magento 2 C# APIs SOAP problem

Seems to be an install issue with Magento 2.1. see here:

 

https://github.com/magento/magento2/issues/5330

 

Edit: Looks like they have hid the WSDL descriptors behind security, thus breaking its statement of being WSDL 1.2 compatible. You need to allow Anonymous access to the SOAP API for you store*-> Build your Objects in Visual Studio (I tied 2015 CE) -> Turn Anon access off.

 

 

*Stores > Configuration > Services > Magento Web API > Web API Security. Then select Yes from the Allow Anonymous Guest Access menu.

Re: Magento 2 C# APIs SOAP problem

Thanks Mark_B97, but when I try to access to the URL you posted I get the following message:

 

"Internal Error. Details are available in Magento log file. Report ID: webapi-57f200c63027b"

Re: Magento 2 C# APIs SOAP problem

I read something about putting a loopback into the server for the server, something your provider will need to do.

 

edit: also edited out some of the new 'security' that breaks Visual Studio WSDL creation tool.

 

Edit file: vendor / magento / module-webapi / Model / AbstractSchemaGenerator.php

 

 public function getListOfServices()
    {
        $listOfAllowedServices = [];
        foreach ($this->serviceMetadata->getServicesConfig() as $serviceName => $service) {
            foreach ($service[ServiceMetadata::KEY_SERVICE_METHODS] as $method)
            {
             //  if ($this->authorization->isAllowed($method[ServiceMetadata::KEY_ACL_RESOURCES]))
             //        {
                    $listOfAllowedServices[] = $serviceName;
                    break;
             //   }
            }
        }
        return $listOfAllowedServices;
    }

 

/***********aaaaaaaaaaand***/

 

protected function getAllowedServicesMetadata($requestedServices)
    {
        $allowedServicesMetadata = [];
        foreach ($requestedServices as $serviceName) {
            $serviceMetadata = $this->getServiceMetadata($serviceName);
            foreach ($serviceMetadata[ServiceMetadata::KEY_SERVICE_METHODS] as $methodName => $methodData) {
                if (!$this->authorization->isAllowed($methodData[ServiceMetadata::KEY_ACL_RESOURCES]))
                {
                   // unset($serviceMetadata[ServiceMetadata::KEY_SERVICE_METHODS][$methodName]);
                }
            }
            if (!empty($serviceMetadata[ServiceMetadata::KEY_SERVICE_METHODS]))
            {
                $this->removeRestrictedRoutes($serviceMetadata);
                $allowedServicesMetadata[$serviceName] = $serviceMetadata;
            }
        }
        return $allowedServicesMetadata;
    }

    /**
     * Remove routes which should not be visible to current user.
     *
     * @param array &$serviceMetadata
     * @return void
     */
    protected function removeRestrictedRoutes(&$serviceMetadata)
    {
        $allowedMethodNames = array_keys($serviceMetadata[ServiceMetadata::KEY_SERVICE_METHODS]);
        /** Remove routes which reference methods not visible to current user */
        if (isset($serviceMetadata[ServiceMetadata::KEY_ROUTES])) {
            foreach ($serviceMetadata[ServiceMetadata::KEY_ROUTES] as $path => &$routeGroup) {
                foreach ($routeGroup as $httpMethod => &$route) {
                    if (!in_array($route[ServiceMetadata::KEY_ROUTE_METHOD], $allowedMethodNames)) {
                   //     unset($routeGroup[$httpMethod]);
                    }
                }
                if (empty($routeGroup)) {
                 // unset($serviceMetadata[ServiceMetadata::KEY_ROUTES][$path]);
                }
            }
        }
    }

 

Re: Magento 2 C# APIs SOAP problem

Thanks a lot Mark_B97,

 

I'll try it. I have no experience in PHP develoment. But I'll try it...

 

Yesterday I tried using REST, and found a package on NuGet that seems to work fine. This package's name is RestSharp. I can connect (with or without a token, it depends on the REST API I use) and call APIs. I have tried calling GET APIs with success. Now I'm going to call some POST APIs to test them... I'll post the results here.

 

Best regards!

Re: Magento 2 C# APIs SOAP problem

I personally suggest use REST APIs for Magento 2, Because a multiple problems in SOAP API's like

 

-Eats up bandwidth for repeated communications with server
-Difficult implementation method
-Cannot be easily understood and used by many developers
-When there are bandwidth limitations

 

A detailed reasons for this api's : https://gist.github.com/hjr3/2289546