Rarm
Rarm

Reputation: 101

Add authorization permanent token header to POST JSON using HttpClient in REST API

I am trying to POST a JSON with HhttpClient using a permanent token authorization, but I always get an 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 this:

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

The token seems to be fine. It is an alphanumeric string all lower case. Is this the correct way to use a permanent token?

update The key i have to use for the header is: IDENTITY_KEY but still failing

Upvotes: 0

Views: 2706

Answers (1)

Rarm
Rarm

Reputation: 101

finally it worked with Add.. had to use this 'key' value rather than authorization in the header:

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

Upvotes: 1

Related Questions