eSocialBX: error 402- " invalid request"

I'm trying to consume the query web service to event identifiers, but I get this return:

<?xml version="1.0"?>
<eSocial xmlns="http://www.esocial.gov.br/schema/consulta/identificadores-eventos/retorno/v1_0_0">
  <retornoConsultaIdentificadoresEvts>
    <status>
      <cdResposta>402</cdResposta>
      <descResposta>Solicitação inválida. Redefina sua consulta</descResposta>
    </status>
  </retornoConsultaIdentificadoresEvts>
</eSocial>

I am sending the following XML:

<?xml version="1.0" encoding="utf-8"?>
<eSocial xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.esocial.gov.br/schema/consulta/identificadores-eventos/empregador/v1_0_0">
  <consultaIdentificadoresEvts>
    <ideEmpregador>
      <tpInsc>1</tpInsc>
      <nrInsc>00000000000000</nrInsc>
    </ideEmpregador>
    <consultaEvtsEmpregador>
      <tpEvt>S-1000</tpEvt>
      <perApur>2018</perApur>
    </consultaEvtsEmpregador>
  </consultaIdentificadoresEvts>
  <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
    <SignedInfo>
      <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
      <SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" />
      <Reference URI="">
        <Transforms>
          <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
          <Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
        </Transforms>
        <DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" />
        <DigestValue>7vfFTl1HHrHL5V/fTfL5Bmq3gUOarDwzGPhBCwrDKQ0=</DigestValue>
      </Reference>
    </SignedInfo>
    <SignatureValue>...</SignatureValue>
    <KeyInfo>
      <X509Data>
        <X509Certificate>...</X509Certificate>
      </X509Data>
    </KeyInfo>
  </Signature>
</eSocial>

This is the code that generated the XML above (I hid the CNPJ):

static void Main(string[] args)
{
   // Cria o objeto
   ServicoConsultarIdentificadoresEventosClient client = new ServicoConsultarIdentificadoresEventosClient();
   Modelos.ConsultaIdentificadoresEventos.eSocial esocial = new Modelos.ConsultaIdentificadoresEventos.eSocial();
   esocial.consultaIdentificadoresEvts = new eSocialConsultaIdentificadoresEvts();
   esocial.consultaIdentificadoresEvts.ideEmpregador = new TIdeEmpregador();
   esocial.consultaIdentificadoresEvts.ideEmpregador.tpInsc = 1;
   esocial.consultaIdentificadoresEvts.ideEmpregador.nrInsc = "00000000000000";
   esocial.consultaIdentificadoresEvts.consultaEvtsEmpregador = new TConsultaEventosEmpregador();
   esocial.consultaIdentificadoresEvts.consultaEvtsEmpregador.perApur = "2018";
   esocial.consultaIdentificadoresEvts.consultaEvtsEmpregador.tpEvt = "S-1000";

   // Carrega o certificado 
   X509Certificate2 certificado = new X509Certificate2();
   X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
   store.Open(OpenFlags.ReadOnly);
   certificado = store.Certificates[1];

   // Serializa o objeto
   var xml = Serialize(esocial);

   // Adiciona o certificado
   SignXmlDoc(xml, certificado);

   // Gera o arquivo XML no disco
   XmlSerializer xs = new XmlSerializer(typeof(XmlDocument));
   TextWriter txtWriter = new StreamWriter(@"C:\\temp\\Serialization.xml");
   xs.Serialize(txtWriter, xml);
   txtWriter.Close();

   // Define o certificado que será usado na chamada do WS
   client.ClientCredentials.ClientCertificate.Certificate = certificado;

   // Converte de document para element
   XDocument linqXml = XDocument.Parse(xml.OuterXml);

   // Chama o serviço enviando o XML      
   var retorno = client.ConsultarIdentificadoresEventosEmpregador(linqXml.Root);

   // Deserializa o objeto
   Modelos.RetornoIndeitificadoresEventos.eSocial deserialized = new Modelos.RetornoIndeitificadoresEventos.eSocial();
   StringReader reader = new StringReader(retorno.ToString());
   XmlSerializer xmlSerializer = new XmlSerializer(typeof(Modelos.RetornoIndeitificadoresEventos.eSocial));
   deserialized = (Modelos.RetornoIndeitificadoresEventos.eSocial)xmlSerializer.Deserialize(reader);
   client.Close();
}

I use xsd.Exe and svcutil to generate the codes.
Has anyone ever gotten this error and knows how to solve or get more details?

Author: Pedro Gaspar, 2019-05-27

1 answers

In addition to the tip I gave in the comments, for you to remove the xmlns:xsi and xmlns:xsd attributes from the eSocial tag, before signing (see item 3 of this answer: https://pt.stackoverflow.com/a/348662/86952 ), I also noticed that in the tag nrInsc (in ideEmpregador) you are putting 14 zeros, but I believe that in this field you should put the employer root CNPJ, with 8 digits.

That's not the problem anyway. I ran the query here and got the same error that you.

From the beginning this eSocialBX documentation (developer Manual v1.7 ) is pretty confusing and I never understood exactly where the S-1000 fit exactly, and where the employer query method (which is what you're using) Fit as well, because the manual says:

The consultarIdentificadoresEventosEmpregador method allows query to identifiers of employer events that do not fall into the categories of Table events or periodic events and do not worker's journals.

Oras, only the S-1000 would be left, if we viewed it as an 'initial event' and not a table event, although it has exactly the same structure as the other table events. But then, why would this method have a field to inform the type of the event (tag tpEvt)?

And to make the confusion even worse, in the same manual, in the method Section consultarIdentificadoresEventosTabela there is an example with the S-1000 event, which always led me to believe that the S-1000 query should in fact be made using the method for tables.

Good, but after all, what is the consultarIdentificadoresEventosEmpregador method for?? After thinking a little and doing some tests, I finally found out. Contrary to what the manual says, this method works to consult the periodic events that do not refer to some worker, that is, those that refer to the employer. By logic, and by the tests I did, they are:

  • S-1250 - production acquisition Rural
  • S-1260 - marketing of Rural production natural person
  • S-1270 - hiring of non-Port bulk workers
  • S-1280 - supplementary information to periodic events
  • S-1295 - totalization request for contingency payment
  • S-1298 - reopening of periodic events
  • S-1299 - closing events Newspapers
  • S-1300 - Employers ' Union Contribution

I could not complete the test on account of the limit of 10 daily queries, but as far as I understood the employer's totalizing events S-5012 and S-5013 can also be queried by this employer method, but, interestingly, the event S-5011 was not accepted by the method. I even sent a message to the eSocial team asking why this behavior (and complaining a little about the immense confusion of the manual's explanations).

Conclusion:
To query the S-1000 event, use the consultarIdentificadoresEventosTabela method. An example of XML:

<?xml version="1.0" encoding="utf-8"?>
<eSocial xmlns="http://www.esocial.gov.br/schema/consulta/identificadores-eventos/tabela/v1_0_0">
   <consultaIdentificadoresEvts>
      <ideEmpregador>
         <tpInsc>1</tpInsc>
         <nrInsc>########</nrInsc>
      </ideEmpregador>
      <consultaEvtsTabela>
         <tpEvt>S-1000</tpEvt>
      </consultaEvtsTabela>
   </consultaIdentificadoresEvts>
</eSocial>

Plugin:
When I was reviewing the answer I noticed a detail I had never noticed in the documentation:

The consultarIdentificadoresEventosEmpregador method allows query to employer event identifiers that do not fall under the table event categories or periodic events and non-periodic of the worker .

That is, it is there, but it is so subtle that I imagine that no one had attempted this detail.

 0
Author: Pedro Gaspar, 2019-05-28 16:26:10