Setting Authorization Header for HttpClient in C# (OAuth, REST)
Hello everyone! I'm new to C# and trying to send a GET request to a REST API that requires authentication via OAuth 2.0. I have a Bearer token (access_token) that I receive from the authorization server. The problem is that I can't properly set the Authorization header for HttpClient. The API returns a 401 Unauthorized error, even though the token is definitely working (I tested it in Postman). My code looks like this:\n\n```csharp\nusing (var client = new HttpClient())\n{\n client.BaseAddress = new Uri("https://api.example.com/");\n client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);\n var response = await client.GetAsync("/v1/users");\n // ... response handling\n}\n```\n\nI also tried adding the header manually via `client.DefaultRequestHeaders.Add("
No answers yet.
Log in to leave an answer