Others

How to Use HttpClient with a Bearer Token for SharePoint Online

Natalie TaylorNatalie Taylor Oct 22, 2026 869 views

Question details

The user wants to authenticate and call SharePoint Online REST APIs using a C# HttpClient and a Bearer Token to access and retrieve site data.

How to Use HttpClient with a Bearer Token for SharePoint Online
Product
SharePoint Online
Device & OS
not provided
Scenario
Developing a C# application to interact programmatically with SharePoint Online site data, requiring secure REST API authentication.
Observed behavior
The developer needs the correct implementation of the authorization header and API endpoint structure to successfully authenticate and fetch SharePoint data.
Before you start

Ensure your application is registered in Microsoft Entra ID (Azure AD) to securely obtain the required client credentials and bearer access token.

Solution 1Recommended

Implement HttpClient Authorization Header in C#

Configure your C# HttpClient to include the bearer token in the request headers before making SharePoint REST API calls.

When interacting with SharePoint Online REST APIs, your application must provide a valid OAuth 2.0 access token (Bearer token) within the HTTP request header. This verifies your application's identity and grants access to the specified site resources.

1
Initialize the HttpClient

Create a new instance of HttpClient in your C# application, or inject it using an IHttpClientFactory for better resource management.

2
Set the Authorization Header

Access the DefaultRequestHeaders.Authorization property of your HttpClient. Set it to a new AuthenticationHeaderValue, passing 'Bearer' and your token string as parameters.

3
Configure the API Endpoint

Specify the correct SharePoint Online REST API endpoint URL, ensuring it matches your tenant and site format (e.g., https://{tenant}.sharepoint.com/sites/{site}/_api/web/lists).

4
Execute the Request

Use methods like GetAsync or PostAsync to execute the API call, then read and deserialize the JSON response to process your site data.

Implement HttpClient Authorization Header in C#
Token Expiration: Bearer tokens typically expire after one hour. Be sure to implement a token refresh mechanism in your code to ensure uninterrupted API access.
Free Microsoft Office alternative

Simplify Your Document Workflow with WPS Office

While SharePoint handles enterprise API integrations and server-side data, WPS Office provides a lightweight, highly compatible, and free alternative for everyday document creation. Seamlessly handle Word, Excel, and PowerPoint files without a heavy subscription.

Fully compatible with Microsoft Office formats (.docx, .xlsx, .pptx)Lightweight desktop and mobile application that runs smoothly on most devicesBuilt-in robust PDF editing, merging, and conversion toolsFamiliar user interface requiring zero learning curve for new users
microsoft office alternative - wps office

Frequently Asked Questions

What permissions are required to access SharePoint REST APIs using a Bearer token?

Your application must be granted specific API permissions (such as Sites.Read.All or Sites.Manage.All) through Microsoft Entra ID (Azure AD). An administrator must also grant tenant-wide admin consent for these permissions before the token will be accepted.

Why am I receiving a 401 Unauthorized error with my HttpClient?

A 401 error usually indicates that the Bearer token is expired, invalid, or missing the necessary scopes. Ensure you are sending the token in the 'Authorization: Bearer {token}' format and that the token audience exactly matches your SharePoint tenant URL.

How do I format JSON payloads for SharePoint Online POST requests?

When making POST requests to create or update data, you must include an 'accept' header set to 'application/json;odata=verbose' and a 'content-type' header set to 'application/json;odata=verbose'. Ensure your JSON payload matches the specific entity type required by SharePoint.