How to use a permanent token with Http POST in REST services

2

Good morning,

I'm trying to do a POST of a Json with a permanent token, but it always gives me error 401 unauthorized , code:

    public static async Task<Uri> CrearitemAsync(Item item)
    {
    using (var client = new HttpClient())
    {
    client.BaseAddress = new Uri(BaseUri);
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("OAuth", AuthToken);
    HttpResponseMessage response = await client.PostAsJsonAsync("items/" + IdProvider, JsonConvert.SerializeObject(item));
    response.EnsureSuccessStatusCode();
    return response.Headers.Location;
    }
    }

I also tried with:

client.DefaultRequestHeaders.Add("Authorization", "Bearer " + AuthToken);

and with:

 var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://ejemplo.com/items/id");
        httpWebRequest.PreAuthenticate = true;
        httpWebRequest.Headers.Add("Authorization", "IDENTITY_KEY" + AuthToken);
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = "POST";
        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            streamWriter.Write(JsonConvert.SerializeObject(item));
            streamWriter.Flush();
            streamWriter.Close();
        }

        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var result = streamReader.ReadToEnd();
        }

The token seems fine, it is an alphanumeric string with all lowercase,

update the key that they have passed to me is: IDENTITY_KEY but I still fail

Any ideas?

    
asked by Rarm 26.09.2017 в 16:30
source

1 answer

1

In the end I work with Add:

client.DefaultRequestHeaders.Add("IDENTITY_KEY", AuthToken);

This happens because the service did not use the Authorization header, it was necessary to pass only the 'key' of the header as 'IDENTITY_KEY', so in the headers it remains as IDENTITY_KEY: token

    
answered by 27.09.2017 / 14:31
source