I already managed to solve my question about the token, now what I have problems is how I can upload a file of txt format that I have saved on my disk D in a folder called SENDSUNAT \ doc.txt once I return that, I generate a ticket to which I concatenate it and they will return me 3 xml, zip and excel files that I want to save in a different folder ... Could someone help me?
//VARIABLES
const string userName = "mi usuario";
const string password = "mi clave";
const string apiBaseUri = "https://ose-gw1.efact.pe:443";
const string apiGetPeoplePath = "/api-efact-ose/oauth";
const string authorization = "Y2xpZW50OnNlY3JldA==";
const string tempurl = @"D:\SENDSUNAT\doc.txt";
const string tempurl2 = "doc.txt";
static void Main(string[] args)
{
//OBTENER EL TOKEN
var token = GetAPIToken(userName, password, apiBaseUri).Result;
Console.WriteLine("Token: {0}", token);
//HACER EL LLAMADO
var response = GetRequest(token, apiBaseUri, apiGetPeoplePath).Result;
Console.WriteLine("response: {0}", response);
//ESPERAR LA CLAVE
Console.ReadKey();
}
private static async Task<string> GetAPIToken(string userName, string password, string apiBaseUri)
{
using (var client = new HttpClient())
{
//SETUP CLIENTE
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authorization);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//SETUP LOGIN DATA
var formContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("username", userName),
new KeyValuePair<string, string>("password", password),
});
//ENVIAR REQUEST
HttpResponseMessage response = await client.PostAsync("https://ose-gw1.efact.pe:443/api-efact-ose/oauth/token", formContent);
//OBTENER EL ACCESO TOKEN DEL RESPONSE BODY
var responseJson = await response.Content.ReadAsStringAsync();
var jObject = JObject.Parse(responseJson);
return jObject.GetValue("access_token").ToString();
}
}
static async Task<string> GetRequest(string token, string apiBaseUri, string requestPath)
{
using (var client = new HttpClient())
{
//SETUP CLIENTE
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
//var cont = new MultipartFormDataContent();
//cont.Add(new StreamContent(new MemoryStream()), "file", tempurl);
//var cont = new MultipartFormDataContent();
//var fileContent = new ByteArrayContent(System.IO.File.ReadAllBytes(tempurl));
//cont.Add(fileContent);
//var cont = new ByteArrayContent(System.IO.File.ReadAllBytes(tempurl));
//Stream fileStream = System.IO.File.OpenRead("D:\SENDSUNAT\" + tempurl2);
//var cont = new MultipartFormDataContent();
//cont.Add(new ByteArrayContent(File.ReadAllBytes(tempurl)));
//var cont = new MultipartFormDataContent();
var method = new MultipartFormDataContent();
const string fileName = "D:\SENDSUNAT\doc.txt";
var streamContent = new StreamContent(File.Open(fileName, FileMode.Open));
method.Add(streamContent, "file");
//HACER EL REQUEST
HttpResponseMessage response = await client.PostAsync("https://ose-gw1.efact.pe:443/api-efact-ose/v1/document", method);
var responseString = await response.Content.ReadAsStringAsync();
return responseString;
}
}