401 Unauthorized Error When Using Personal API Token Pipedrive API

I’m trying to access the Pipedrive API in C#, but I’m only getting a 401 error in return. Here’s my code:

static async Task Main()
{
string url = “Log in”;

string apiToken = "MyToken"; 

using (HttpClient client = new HttpClient())
{
    client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiToken}");
    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

    try
    {
        HttpResponseMessage response = await client.GetAsync(url);

        if (response.IsSuccessStatusCode)
        {
            string responseBody = await response.Content.ReadAsStringAsync();
            Console.WriteLine(responseBody);
        }
        else
        {
            Console.WriteLine($"Request error: {response.StatusCode} - {response.ReasonPhrase}");
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error: {ex.Message}");
    }
}

}

Does anyone know how I can fix this?

Hi! It looks like you’re trying to use an API token. Please try providing it using the x-api-token header (instead of Bearer).

You can check out the documentation for more information: Authentication