consumir web service SOAP con TOKEN en delphi
Hola amigos del foro,
Acudo a ustedes por la inexperiencia que tengo en consumir servicios web y peor todavía con un token que no se envía como parámetro, espero que ayuden , de ante mano les agradezca mucho.
Esto es el ejemplo que pone el proveedor en NETCORE y PHP y algo de recomendación que les deja abajo.
Para realizar el consumo de los servicios que solicitan la autenticación mediante el uso de token, se debe considerar en el header del servicio el parámetro: Authorization y el valor: “Token VALORTOKEN”. Donde la variable “VALORTOKEN” es el Token que se obtuvo a través del servicio de autenticación.
Nota.- La inclusión del Token de la petición SOAP debe hacerse en la cabecera HTTP y no así en la cabecera XML del request
Para efectos ilustrativos, se ejemplifica en los siguientes lenguajes:
Código en PHP:
<?php
/** Ejemplo de manejo de SOAP con libreria de PHP php-soap */
$wsdl = "https://pilotosiatservicios.impuestos.gob.bo/v1/FacturacionCodigos?wsdl";
$token = 'VALOR_TOKEN';
$opts = array(
'http' => array(
'header' => "Authorization: Token $token",
)
);
$context = stream_context_create($opts);
$client = new \SoapClient($wsdl, [
'stream_context' => $context,
'cache_wsdl' => WSDL_CACHE_NONE,
'compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP | SOAP_COMPRESSION_DEFLATE,
// other options
]);
$respons = $client->verificarComunicacion();
?>
Código en .NETCORE:
using System;
using System.ServiceModel;
using ServiceReference;
using System.Xml;
using System.Threading.Tasks;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Channels;
namespace client
{
class Program
{
private static string endpointAddress = "https://pilotosiatservicios.impuestos.gob.bo/v1/FacturacionCodigos?wsdl";
static void Main(string[] args)
{
string token = "TOKEN";
BasicHttpBinding binding = new BasicHttpBinding
{
SendTimeout = TimeSpan.FromSeconds(1000),
MaxBufferSize = int.MaxValue,
MaxReceivedMessageSize = int.MaxValue,
AllowCookies = true,
ReaderQuotas = XmlDictionaryReaderQuotas.Max
};
binding.Security.Mode = BasicHttpSecurityMode.Transport; // https
//binding.Security.Mode = BasicHttpSecurityMode.None; // http
EndpointAddress address = new EndpointAddress(endpointAddress);
ServicioFacturacionCodigosClient servicio = new ServicioFacturacionCodigosClient(binding, address);
servicio.Endpoint.EndpointBehaviors.Add(new CustomAuthenticationBehaviour($"Token {token}"));
try {
Task<verificarComunicacionResponse> resp = servicio.verificarComunicacionAsync();
resp.Wait();
Console.WriteLine(resp.Result.@return);
} catch (Exception e) {
Console.WriteLine($"{e.Message}");
}
}
}
public class CustomMessageInspector : IClientMessageInspector
{
readonly string _authToken;
public CustomMessageInspector(string authToken)
{
_authToken = authToken;
}
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
var reqMsgProperty = new HttpRequestMessageProperty();
reqMsgProperty.Headers.Add("Authorization", _authToken);
request.Properties[HttpRequestMessageProperty.Name] = reqMsgProperty;
return null;
}
public void AfterReceiveReply(ref Message reply, object correlationState)
{ }
}
}
|