如何在.Net应用程序中配置基于令牌的Magento 2.0 SOAP API对象的身份验证 [英] How do i Configure the token based authentication of Magento 2.0 SOAP API object in .Net application

查看:95
本文介绍了如何在.Net应用程序中配置基于令牌的Magento 2.0 SOAP API对象的身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在.Net应用程序中使用新引入的Magento 2.0 SOAP API.但是随着wsdl的新结构化端点的变化,函数调用执行与早期版本稍有不同.

i am trying to consume the newly introduced Magento 2.0 SOAP API in .Net application. but as per the newly structured endpoints wsdl changes,function calling execution are little bit different from the earlier version..

是否有人亲自使用和调用.net应用程序中Magento 2.0的Web API Soap对象功能?

Is Anyone has hands on in consumption and calling of web API Soap object function of Magento 2.0 in .Net application??

如果可以,请提供相同的代码段.

if yes can you provide some snippet code of the same.

提前谢谢!

推荐答案

我终于让Magento 2 SOAP API和.NET互相交谈.这是对我有用的分步指南:

I finally got the Magento 2 SOAP API and .NET talking to each other. Here is the step-by-step that worked for me:

系统>集成>添加新集成

System > Integrations > Add New Integration

仅在此处填写姓名和电子邮件,因为我们只想发回到我们自己的商店,并让Magento为您保存令牌和密钥.不要忘记在单独的选项卡上设置集成权限.

Only fill out name and email here since we just want to post back to our own store and let Magento save the tokens and keys for you. Don't forget to set your permissions for the integration on the separate tab.

注意:如果您使用的是VM,请确保/etc/hosts文件本身具有一个条目,因为Web服务器将回发到其自身.

NOTE: If you're using a VM make sure your /etc/hosts file has an entry for itself since the web server will be posting back to itself.

您应该在响应中显示一个访问令牌.记下以备后用.

You should have an access token displayed in the response. Make a note of it for later.

在项目解决方案资源管理器中,右键单击引用",然后选择添加服务引用" 地址将类似于:

In your project Solution Explorer, right click References and choose Add Service Reference The address will be something like:

http://MyMagentoStore.com/soap/default?wsdl= 1& services = catalogProductAttributeGroupRepositoryV1,catalogProductAttributeManagementV1

services =后面是您要使用的服务的逗号分隔列表.

Where services= is followed by a comma-delimited list of the services you will be consuming.

要查看所有可用服务,请在商店中访问以下网址:

To see all available services, visit the following url in your store:

http://MyMagentoStore.com/soap/default?wsdl_list=1

我不建议您全部选择它们,因为这会使SOAP调用URL变得非常长.最后,我将服务分成了catalogProduct,customer等部分,并为每个集合创建了单独的服务引用.

I wouldn't recommend choosing all of them since it will make the SOAP call URLs extremely long. I ended up grouping the services into sections like catalogProduct, customer, etc and creating indivual services references for each set.

构造您的URL,并将其粘贴到添加服务参考"对话框中,然后单击执行".

Construct your URL and paste it into the Add Service Reference dialog and click Go.

如果您确实选择像我这样拆分服务,只需给每个服务一个好的名称空间即可.我使用了Magento2Soap.Customer,Magento2Soap.CatalogProduct等.无论如何,请在对话框底部选择一个名称空间,然后单击确定".这将生成代码以连接到Magento.

If you did choose to break the services apart like me, just give each service a good namespace. I went with Magento2Soap.Customer, Magento2Soap.CatalogProduct, etc. In any case, choose a namespace at the bottom of the dialog and click OK. This will generate the code to connect to Magento.

现在最困难的部分要弄清楚:实际上是可以正常工作的.

Now the hardest part to figure out: Actually getting it to work.

您必须使用WCF库在SOAP调用中添加正确的Authorization Header.这样做并不明显.这是一个代码段:

You have to use the WCF libraries to add the proper Authorization Header in your SOAP calls. Doing this was not obvious. Here is a snippet:

    var client = new customerCustomerRepositoryV1PortTypeClient();
    client.Endpoint.Binding = new BasicHttpBinding();

    HttpRequestMessageProperty hrmp = new HttpRequestMessageProperty();
    hrmp.Headers.Add("Authorization", "Bearer " + yourAccessToken);

    OperationContextScope contextScope = new OperationContextScope(client.InnerChannel);
    OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = hrmp;

    CustomerCustomerRepositoryV1GetByIdResponse response = client.customerCustomerRepositoryV1GetById(
        new CustomerCustomerRepositoryV1GetByIdRequest() { 
            customerId = 1 
        }
    );
    Console.WriteLine(response.result.firstname);

请注意,您将需要添加对System.ServiceModel的项目引用.您可以通过在解决方案资源管理器中右键单击引用",然后单击添加引用"来实现.它将在核心库列表中.

Note that you will need to add a project reference to System.ServiceModel. You can do this by right-clicking References in your Solution Explorer and then Add Reference. It will be in the list of core libraries.

我从来没有找到一种对每种不同类型的调用使用多态的好方法,因为生成的类不继承自任何公共类或接口,并且我不会接近动态类型.我最终制作了一个静态类来简化事情:

I never found a great way to use polymorphism for each different type of call since the generated classes don't inherit from any common class or interface, and I wasn't going to go near the dynamic type. I ended up making a static class to simplify things:

public static class MagentoSOAP {

    private static BasicHttpBinding GetBinding() {
        return new BasicHttpBinding();
    }

    private static void AddAuthorizationHeader(IClientChannel clientChannel) {
        HttpRequestMessageProperty hrmp = new HttpRequestMessageProperty();
        hrmp.Headers.Add("Authorization", "Bearer " + Constants.MAGENTO_SOAP_ACCESS_TOKEN);

        OperationContextScope contextScope = new OperationContextScope(clientChannel);
        OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = hrmp;
    }

    public static customerCustomerRepositoryV1PortTypeClient Customer {
        get {
            var client = new customerCustomerRepositoryV1PortTypeClient();
            client.Endpoint.Binding = GetBinding();
            AddAuthorizationHeader(client.InnerChannel);
            return client;
        }
    }

    public static catalogProductRepositoryV1PortTypeClient Product {
        get {
            var client = new catalogProductRepositoryV1PortTypeClient();
            client.Endpoint.Binding = GetBinding();
            AddAuthorizationHeader(client.InnerChannel);
            return client;
        }
    }
}

在实践中:

var product = MagentoSOAP.Product.catalogProductRepositoryV1Get(new Magento2SOAP.CatalogProduct.CatalogProductRepositoryV1GetRequest() {
    sku = "My Product SKU"
});
int id = product.result.id;

我希望这会有所帮助.我欢迎任何改进或建议.

I hope this is helpful. I welcome any improvements or suggestions.

这篇关于如何在.Net应用程序中配置基于令牌的Magento 2.0 SOAP API对象的身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆