皇家邮政运输API v2 SOAP认证c#

本文关键字:SOAP 认证 v2 API 运输 皇家 | 更新日期: 2023-09-27 18:02:02

我已经完成了大多数基于其他人的堆栈溢出代码的工作。见下文.

我目前的问题是,我仍然得到授权失败,我可以清楚地看到为什么。v2 API需要传递X-IBM-Client-Id和X-IBM-Client-Secret以及SOAP安全头。但是,我不知道如何在使用Wsdl文件创建的服务时注入它。

解决问题:

克服了名称空间问题(使用消息格式化器在c#控制台应用程序中使用WCF皇家邮件API)。

解决绑定配置问题,导致两个安全头。此外,如果您希望在检索标签时不出现异常,则必须设置maxReceivedMessageSize。最后结合:

<system.serviceModel> <bindings> <basicHttpBinding> <binding name="basicBindingTransport" maxReceivedMessageSize="2147483647"> <security mode="Transport"> </security> </binding> </basicHttpBinding> </bindings> <client> <endpoint address="https://api.royalmail.net/shipping/v2" binding="basicHttpBinding" bindingConfiguration="basicBindingTransport" contract="RoyalMailApiWsdl.shippingAPIPortType" name="shippingAPIPort" /> </client> </system.serviceModel>

已解决E0007 Authorization Failure.

解决了客户端身份验证方案为"匿名"的HTTP请求是未经授权的(你必须像上面那样使用安全"传输"绑定,并直接将凭据注入HTTP post头本身(见下面我的答案)。

和许多其他的问题,我现在不记得了。我希望这篇文章能对别人有所帮助。

皇家邮政运输API v2 SOAP认证c#

要在Shipping API v2中实现双重身份验证,您可以使用以下代码(取自如何为使用Axis 1.4 Web服务的c# Web服务客户端添加自定义Http头)

shippingAPIPortTypeClient client = GetProxy();
<..>
using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
{
    var httpRequestProperty = new HttpRequestMessageProperty();
    httpRequestProperty.Headers.Add(@"X-IBM-Client-Id", _credentials.HttpSecurity.ClientId);
    httpRequestProperty.Headers.Add(@"X-IBM-Client-Secret", _credentials.HttpSecurity.ClientSecret);
    OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
    createShipmentResponse response = client.createShipment(GetSecurityHeaderType(), request);
    return response;
}
private shippingAPIPortTypeClient GetProxy()
{
    // binding comes from configuration file
    var shippingClient = new shippingAPIPortTypeClient();
    shippingClient.ClientCredentials.UserName.UserName = _credentials.SoapSecurity.Username;
    shippingClient.ClientCredentials.UserName.Password = _credentials.SoapSecurity.Password;
    shippingClient.ClientCredentials.UseIdentityConfiguration = true;
    foreach (OperationDescription od in shippingClient.Endpoint.Contract.Operations)
    {
        od.Behaviors.Add(new RoyalMailIEndpointBehavior());
    }
    return shippingClient;
}
private SecurityHeaderType GetSecurityHeaderType()
{
    SecurityHeaderType securityHeader = new SecurityHeaderType();
    DateTime created = DateTime.Now;
    string creationDate;
    creationDate = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ");
    string nonce = nonce = (new Random().Next(0, int.MaxValue)).ToString();
    byte[] hashedPassword;
    hashedPassword = GetSHA1(_credentials.SoapSecurity.Password);
    string concatednatedDigestInput = string.Concat(nonce, creationDate, Encoding.Default.GetString(hashedPassword));
    byte[] digest;
    digest = GetSHA1(concatednatedDigestInput);
    string passwordDigest;
    passwordDigest = Convert.ToBase64String(digest);
    string encodedNonce;
    encodedNonce = Convert.ToBase64String(Encoding.Default.GetBytes(nonce));
    XmlDocument doc = new XmlDocument();
    using (XmlWriter writer = doc.CreateNavigator().AppendChild())
    {
        writer.WriteStartDocument();
        writer.WriteStartElement("Security");
        writer.WriteStartElement("UsernameToken", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
        writer.WriteElementString("Username", _credentials.SoapSecurity.Username);
        writer.WriteElementString("Password", passwordDigest);
        writer.WriteElementString("Nonce", encodedNonce);
        writer.WriteElementString("Created", creationDate);
        writer.WriteEndElement();
        writer.WriteEndElement();
        writer.WriteEndDocument();
        writer.Flush();
    }
    doc.DocumentElement.RemoveAllAttributes();
    System.Xml.XmlElement[] headers = doc.DocumentElement.ChildNodes.Cast<XmlElement>().ToArray<XmlElement>();
    securityHeader.Any = headers;
    return securityHeader;
}

在c#中提供完整的RoyalMailShipping API v2解决方案之上:https://github.com/povilaspanavas/RoyalMailShippingApiV2

它有三个测试,一个用于创建本地(GB)货物,另一个用于创建国际货物,第三个用于检索标签pdf文件。