How to Use HttpClient with a Bearer Token for SharePoint Online
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.

- 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.
Ensure your application is registered in Microsoft Entra ID (Azure AD) to securely obtain the required client credentials and bearer access token.
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.
Create a new instance of HttpClient in your C# application, or inject it using an IHttpClientFactory for better resource management.
Access the DefaultRequestHeaders.Authorization property of your HttpClient. Set it to a new AuthenticationHeaderValue, passing 'Bearer' and your token string as parameters.
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).
Use methods like GetAsync or PostAsync to execute the API call, then read and deserialize the JSON response to process your site data.

Seek Assistance in the Microsoft Developer Community
For complex integration issues or advanced REST querying, the official developer community can provide tailored C# coding guidance.
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.

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.




