admin管理员组

文章数量:1295335

I have written a program for an api request, but the API needs a bearer token in the header to return a response. Despite all the variants I have tried, the API keeps telling me that no valid key is being sent. However, a counter-attempt with postman works. Apparently the header that arrives at the API from my request is completely empty. My program looks like this:

    using (HttpClient client = new HttpClient())
{

    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));                    
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "[TOKEN]"); //Version 1 (doesn't work)
    //client.DefaultRequestHeaders.Add("Authorization", "Bearer " + "[TOKEN]"); //Version 2 doesn't work
    HttpResponseMessage response = await client.GetAsync("[API-URI]?ip=[IP]");
    if (response.IsSuccessStatusCode)
    {
        string json = await response.Content.ReadAsStringAsync();
        MessageBox.Show(json, "Rückgabewert");
    }
    else
    {
        string json = await response.Content.ReadAsStringAsync();
        MessageBox.Show(json, "Rückgabewert");
    }
}

The Answer is always

{"errors":["Invalid API key"]} (401)

Here's the description of the used bearer token from postman:

Bearer tokens enable requests to authenticate using an access key, such as a JSON Web Token (JWT). The token is a text string, included in the request header. In the request Authorization tab, select Bearer Token from the Auth Type dropdown list. In the Token field, enter your API key value. For added security, store it in a variable and reference the variable by name.

Postman appends the token value to the text Bearer in the required format to the request Authorization header as follows:

Bearer <Your API key>

If you need a custom prefix, use an API Key with a key of Authorization.

I'm using .NET Framework 4.8

I have written a program for an api request, but the API needs a bearer token in the header to return a response. Despite all the variants I have tried, the API keeps telling me that no valid key is being sent. However, a counter-attempt with postman works. Apparently the header that arrives at the API from my request is completely empty. My program looks like this:

    using (HttpClient client = new HttpClient())
{

    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));                    
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "[TOKEN]"); //Version 1 (doesn't work)
    //client.DefaultRequestHeaders.Add("Authorization", "Bearer " + "[TOKEN]"); //Version 2 doesn't work
    HttpResponseMessage response = await client.GetAsync("[API-URI]?ip=[IP]");
    if (response.IsSuccessStatusCode)
    {
        string json = await response.Content.ReadAsStringAsync();
        MessageBox.Show(json, "Rückgabewert");
    }
    else
    {
        string json = await response.Content.ReadAsStringAsync();
        MessageBox.Show(json, "Rückgabewert");
    }
}

The Answer is always

{"errors":["Invalid API key"]} (401)

Here's the description of the used bearer token from postman:

Bearer tokens enable requests to authenticate using an access key, such as a JSON Web Token (JWT). The token is a text string, included in the request header. In the request Authorization tab, select Bearer Token from the Auth Type dropdown list. In the Token field, enter your API key value. For added security, store it in a variable and reference the variable by name.

Postman appends the token value to the text Bearer in the required format to the request Authorization header as follows:

Bearer <Your API key>

If you need a custom prefix, use an API Key with a key of Authorization.

I'm using .NET Framework 4.8

Share Improve this question edited Feb 12 at 12:03 ASh 35.7k9 gold badges65 silver badges87 bronze badges asked Feb 12 at 10:46 LarianLarian 431 silver badge3 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

I had an almost alike problem a little while ago. See my problem and solution here: ASP.NET Core 8 Web API : JWT token always invalid

TL;DR - Installing the package Microsoft.IdentityModel.JsonWebTokens solved the problem for me

Found the answer: we modified the backend to give back the header as answer and found out that "Authentication" was removed from header (all other header-keys were there) and so i found an solution for my problem on this site here:

Authorization Headers is missing using c# client

本文标签: cApiRequest with Bearer Token doesn39t workStack Overflow