我如何在 .Net 应用程序中配置基于令牌的 Magento 2.0 SOAP API object 身份验证
How do i Configure the token based authentication of Magento 2.0 SOAP API object in .Net application
我正在尝试在 .Net 应用程序中使用新引入的 Magento 2.0 SOAP API。但根据新结构的端点 wsdl 更改,函数调用执行与早期版本略有不同..
有没有人亲身体验过web的消费和调用API.Net应用中Magento 2.0的Soap object功能??
如果是,您能否提供一些相同的代码片段。
提前致谢!
我终于让 Magento 2 SOAP API 和 .NET 相互对话了。这是对我有用的step-by-step:
在 Magento 2 后端
系统 > 集成 > 添加新集成
这里只填写姓名和电子邮件,因为我们只想 post 回到我们自己的商店,让 Magento 为您保存令牌和密钥。不要忘记在单独的选项卡上设置您的集成权限。
注意:如果您使用的是虚拟机,请确保您的 /etc/hosts 文件有一个条目,因为网络服务器将 post 返回到它自己。
您应该在响应中显示一个访问令牌。记录下来以备后用。
2013 年 Visual Studio
在您的项目解决方案资源管理器中,右键单击“引用”并选择“添加服务引用”
地址将类似于:
其中 services= 后跟 comma-delimited 您将使用的服务列表。
要查看所有可用服务,请访问您商店中的以下 url:
http://MyMagentoStore.com/soap/default?wsdl_list=1
我不建议选择所有这些,因为这会使 SOAP 调用 URL 变得非常长。我最终将服务分组为 catalogProduct、客户等部分,并为每组创建单独的服务参考。
构建您的 URL 并将其粘贴到“添加服务引用”对话框中,然后单击“开始”。
如果您确实像我一样选择拆分服务,只需为每个服务提供一个良好的名称空间即可。我选择了 Magento2Soap.Customer、Magento2Soap.CatalogProduct 等。无论如何,请在对话框底部选择一个名称空间,然后单击“确定”。这将生成连接到 Magento 的代码。
实际上与 Magento 对话成功
现在是最难弄清楚的部分:真正让它发挥作用。
您必须使用 WCF 库在 SOAP 调用中添加正确的授权 Header。这样做并不明显。这是一个片段:
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 的项目引用。您可以通过解决方案资源管理器中的 right-clicking 引用然后添加引用来执行此操作。它将在核心库列表中。
我从来没有找到对每种不同类型的调用使用多态性的好方法,因为生成的 classes 不会从任何公共 class 或接口继承,而且我不会去去接近动态类型。我最终制作了一个静态 class 来简化事情:
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;
希望对您有所帮助。我欢迎任何改进或建议。
我正在尝试在 .Net 应用程序中使用新引入的 Magento 2.0 SOAP API。但根据新结构的端点 wsdl 更改,函数调用执行与早期版本略有不同..
有没有人亲身体验过web的消费和调用API.Net应用中Magento 2.0的Soap object功能??
如果是,您能否提供一些相同的代码片段。
提前致谢!
我终于让 Magento 2 SOAP API 和 .NET 相互对话了。这是对我有用的step-by-step:
在 Magento 2 后端
系统 > 集成 > 添加新集成
这里只填写姓名和电子邮件,因为我们只想 post 回到我们自己的商店,让 Magento 为您保存令牌和密钥。不要忘记在单独的选项卡上设置您的集成权限。
注意:如果您使用的是虚拟机,请确保您的 /etc/hosts 文件有一个条目,因为网络服务器将 post 返回到它自己。
您应该在响应中显示一个访问令牌。记录下来以备后用。
2013 年 Visual Studio
在您的项目解决方案资源管理器中,右键单击“引用”并选择“添加服务引用” 地址将类似于:
其中 services= 后跟 comma-delimited 您将使用的服务列表。
要查看所有可用服务,请访问您商店中的以下 url:
http://MyMagentoStore.com/soap/default?wsdl_list=1
我不建议选择所有这些,因为这会使 SOAP 调用 URL 变得非常长。我最终将服务分组为 catalogProduct、客户等部分,并为每组创建单独的服务参考。
构建您的 URL 并将其粘贴到“添加服务引用”对话框中,然后单击“开始”。
如果您确实像我一样选择拆分服务,只需为每个服务提供一个良好的名称空间即可。我选择了 Magento2Soap.Customer、Magento2Soap.CatalogProduct 等。无论如何,请在对话框底部选择一个名称空间,然后单击“确定”。这将生成连接到 Magento 的代码。
实际上与 Magento 对话成功
现在是最难弄清楚的部分:真正让它发挥作用。
您必须使用 WCF 库在 SOAP 调用中添加正确的授权 Header。这样做并不明显。这是一个片段:
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 的项目引用。您可以通过解决方案资源管理器中的 right-clicking 引用然后添加引用来执行此操作。它将在核心库列表中。
我从来没有找到对每种不同类型的调用使用多态性的好方法,因为生成的 classes 不会从任何公共 class 或接口继承,而且我不会去去接近动态类型。我最终制作了一个静态 class 来简化事情:
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;
希望对您有所帮助。我欢迎任何改进或建议。