Antes de llamar a los Web Services de ARCA, necesitás un ticket de acceso (TA), también llamado Token Authorization.
Este TA expira tras un tiempo, pero no tenés que preocuparte por renovarlo: Afip SDK lo cachea y solicita uno nuevo automáticamente si es necesario. Solo llamá a la API y Afip SDK te devuelve el TA correspondiente.
Para obtener el TA se necesita un certificado digital, pero con Afip SDK podés empezar en modo desarrollo usando el CUIT 20-40937847-2 sin necesidad de uno.
token y sign corresponden el TA que nos da ARCA. Debemos enviar estos datos en el body de cada solicitud a los Web Services. En el manual de cada Web Service verás dónde colocarlos.
3. Llamar al método del Web Service que necesites
Con el TA ya podés realizar las llamadas a los Web Services que necesites.
Es similar al código para obtener el TA, pero debés cambiar la URL y el body de la request.
Podés encontrar las URLs y los body correspondientes en las pestañas API de la documentación de cada Web Service.
using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Newtonsoft.Json;
var client = new HttpClient();
var authUrl = "https://app.afipsdk.com/api/v1/afip/auth";
// Reemplazar con tu access_token obtenido de https://app.afipsdk.com
var accessToken = "TU_TOKEN_AQUI";
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {accessToken}");
var authData = new
{
environment = "dev",
tax_id = "20409378472", // CUIT a utilizar
wsid = "wsfe"
};
var authJsonContent = JsonConvert.SerializeObject(authData);
var authContent = new StringContent(authJsonContent, Encoding.UTF8, "application/json");
var authResponse = await client.PostAsync(authUrl, authContent);
if (!authResponse.IsSuccessStatusCode)
{
Console.WriteLine($"Error en la autorización: {authResponse.StatusCode}");
return;
}
var authResponseBody = await authResponse.Content.ReadAsStringAsync();
dynamic auth = JsonConvert.DeserializeObject(authResponseBody);
string token = auth.token;
string sign = auth.sign;
Console.WriteLine($"Token: {token}");
Console.WriteLine($"Sign: {sign}");
var authData = new
{
environment = "dev",
tax_id = "20111111112", // CUIT del certificado
wsid = "wsfe",
cert = "-----BEGIN CERTIFICATE-----MIIDRzC...",
key = "-----BEGIN RSA PRIVATE KEY-----MIIEowIBAAKCA..."
};