I’m having trouble with a Blazor Server application where I keep getting a 401 unauthorized error when trying to fetch data from Notion’s API. The weird thing is that the exact same request works perfectly in Postman with my internal integration token.
I’m setting up the Authorization header with my bearer token and the Notion-Version header just like in Postman, but something isn’t working right. Here’s my code:
private async Task FetchNotionPageData()
{
string apiUrl = $"https://api.notion.com/v1/pages/{myPageId}";
httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiToken}");
httpClient.DefaultRequestHeaders.Add("Notion-Version", "2022-06-28");
HttpResponseMessage result = new HttpResponseMessage();
try
{
result = await httpClient.GetAsync(apiUrl);
}
catch (Exception error)
{
statusMessage = $"Exception occurred: {error}";
}
if (result.IsSuccessStatusCode)
{
statusMessage = "Request successful: " + await result.Content.ReadAsStringAsync();
}
else
{
statusMessage = $"Request failed: {result.Content}";
errorReason = $"Failure reason: {result.ReasonPhrase}";
}
}
I’ve tried multiple tutorials and examples but can’t figure out why this isn’t working. Any ideas what might be causing the authorization to fail in my Blazor app when it works fine in Postman?
Create a new HttpClient for each request instead of reusing it. I’ve hit this issue before - the HttpClient was holding onto old headers from previous requests. Also double-check your apiToken value with some logging. I’ve seen tokens get corrupted or cut off when they’re passed around in Blazor apps.
This is probably an HttpClient header issue in Blazor Server. Since Blazor Server keeps state between requests, your HttpClient instance sticks around and headers pile up. Don’t use DefaultRequestHeaders - set headers on each request instead:
using var request = new HttpRequestMessage(HttpMethod.Get, apiUrl);
request.Headers.Add("Authorization", $"Bearer {apiToken}");
request.Headers.Add("Notion-Version", "2022-06-28");
var result = await httpClient.SendAsync(request);
I’ve hit similar auth problems with other APIs when headers got cached or messed with by the framework. Also check your apiToken for hidden characters or encoding problems - tokens copied from browser dev tools sometimes grab extra whitespace that breaks auth.
Had this exact same issue last month and it drove me crazy for hours. You’re probably adding headers to the same HttpClient instance multiple times. Every time your method runs, you’re calling DefaultRequestHeaders.Add() again, which creates duplicate headers and the API rejects it. I fixed it by clearing headers first or checking if they already exist. Try adding httpClient.DefaultRequestHeaders.Clear(); at the start of your method, or better yet, move the header setup to your HttpClient config in Program.cs or wherever you register it as a service. Set the headers once and reuse them. Or create headers per request instead of using DefaultRequestHeaders. The Notion API is super strict about header formatting - any duplicates will cause auth failures even with a valid token.