Home › SharePoint Online › 401 Unauthorized

SharePoint Online · 401 Unauthorized · SharePoint Online / Microsoft 365

SharePoint Online Error 401 Unauthorized in REST API Calls

Root cause: OAuth2 access token expired or invalid; Fix: Refresh tokens or update app registration permissions.

Error Details

FactValueSourceNote
Exact error string401 UnauthorizedBrowser developer tools (F12 > Network tab)->
Error code in Azure AD sign-in logsAADSTS50076Entra admin centre > Monitoring > Sign-in logs
SharePoint ULS correlation ID<correlation-id>401 response 'x-ms-diagnostics' header-> Microsoft support
Registry key involvedHKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\OfficeServer\SharedTools\Search\AdminProxyRegistry Editor
Relevant service URLhttps://<tenant>.sharepoint.com/_api/webNetwork tab in browser developer tools->

Log Files to Check

What This Error Actually Means

The HTTP status code "401 Unauthorized" indicates that the request requires user authentication or a valid bearer token, which was either missing from the request headers or has expired. In the context of SharePoint Online and Microsoft 365, this error is raised when an application or service attempts to access resources without providing proper credentials or tokens. This can occur during REST API calls, CSOM requests, or even browser sessions where a user tries to log in but fails due to token validation issues.

Affected Versions and Scope

This issue affects all versions of SharePoint Online and Microsoft 365 tenants that utilize OAuth for authentication. It is particularly relevant when applications are configured with app-only permissions, use client credentials flow, or rely on user-based tokens (bearer tokens). The problem does not affect users who log in directly through the browser interface without relying on API calls or CSOM requests.

Where To Look First: Logs and Event IDs

To diagnose a 401 Unauthorized error, start by examining the following logs: - **Browser Developer Tools (F12 > Network Tab)**: Look for failed SharePoint REST calls that return an HTTP status of "401 Unauthorized". The response headers will include 'WWW-Authenticate: Bearer' indicating the token realm. - **Azure AD Sign-In Logs**: Navigate to Entra admin center > Monitoring > Sign-in logs and filter by application 'SharePoint Online'. Look for failure events with error codes such as AADSTS50076, which indicate token validation issues. - **SharePoint ULS Logs**: - Path: C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\LOGS\Uls.log - Look for correlation IDs in the HTTP response header 'x-ms-diagnostics' and search for these IDs in the ULS logs to get detailed server-side information. - **IIS Logs**: - Path: C:\inetpub\logs\LogFiles\W3SVC - Search for entries related to HTTP status codes 401, which can provide additional context about failed authentication attempts.

Root Causes

Expired OAuth Access Token

OAuth access tokens in SharePoint Online are valid for approximately 60-90 minutes. If an application does not refresh the token before it expires, subsequent requests will result in a "401 Unauthorized" error. To identify this issue, check Azure AD sign-in logs for AADSTS50076 errors.

Azure AD Conditional Access Policy

Conditional access policies may require multi-factor authentication (MFA) or device compliance before granting access to SharePoint resources. If a service principal cannot satisfy these requirements, it will receive a 401 Unauthorized error. Look for AADSTS50076 errors in Azure AD sign-in logs and check the Conditional Access blade in Entra admin center.

Missing Application Permissions

An application may have been granted SharePoint App-Only permissions but lacks the necessary API permissions to access specific resources. For example, if an app is configured with 'Full Control' permission but does not have the required Sites.Read.All or Sites.ReadWrite.All permissions in its Azure AD app registration, it will encounter a 401 Unauthorized error.

User Password Change

When a user changes their password, existing refresh tokens become invalid. If an application relies on these tokens for authentication, subsequent requests will fail with a 401 Unauthorized status code. Check the Unified Audit Log for events related to token revocation and password changes.

Fix Procedure

  1. Verify OAuth Token Refresh Mechanism: Ensure your application is correctly implementing token refresh logic. Use Connect-PnPOnline -Url "https://tenant.sharepoint.com" -Interactive to manually connect and confirm the OAuth flow works.
  2. Check Conditional Access Policies: Review Entra admin center > Identity Governance > Conditional Access policies to ensure they do not block necessary access for your application. If MFA is required, configure an exception or use a compliant device.
  3. Update Application Permissions: Navigate to Azure AD app registrations and add the missing SharePoint API permissions (e.g., Sites.Read.All) to the application's manifest file.
  4. Reissue Refresh Tokens: If a user has changed their password, reissue refresh tokens for all affected applications. Use Get-PnPContext to check current authentication status and Connect-PnPOnline -Url "https://tenant.sharepoint.com" -Interactive to obtain new credentials.
  5. Review ULS Logs: Search the SharePoint ULS logs for correlation IDs from 401 responses. Use Get-Content C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\LOGS\Uls.log | Select-String -Pattern "" to find relevant entries.
  6. Check IIS Logs: Analyze the IIS logs for 401 errors. Use Get-Content C:\inetpub\logs\LogFiles\W3SVC\*.log | Select-String -Pattern "401" to filter and review.
Be cautious when modifying application permissions or reissuing tokens as it may affect enrolled devices and disrupt user workflows.

Error Code Variants

Code Description Distinguishing Factors
AADSTS50124 User account is disabled. Error occurs when attempting to sign in with a disabled user account. Check Azure AD user status.
AADSTS50134 Invalid token signature. This error indicates that the JWT token's signature is invalid, possibly due to incorrect public key or certificate issues. Check Azure AD app registration certificates and secrets.
AADSTS50136 Token validation failure. Differentiates from AADSTS50076 by indicating a specific issue with token validation logic, such as incorrect claims or malformed tokens. Review Azure AD app registration settings and OAuth configuration.

Verification

After implementing the fixes, verify that the application can successfully authenticate to SharePoint Online without encountering "401 Unauthorized" errors. Use Connect-PnPOnline -Url "https://tenant.sharepoint.com" -Interactive; Get-PnPSite to confirm a successful connection and access to site resources. Additionally, review the Unified Audit Log for events related to token validation and permission changes. Look for entries indicating that the application now has proper permissions or that tokens are being refreshed correctly.

Prevention

To prevent recurrence of 401 Unauthorized errors: - Implement a robust token refresh mechanism in your applications. - Regularly review and update Azure AD Conditional Access policies to ensure they do not inadvertently block necessary access for SharePoint Online resources. - Monitor the Unified Audit Log for suspicious activity related to token revocation or permission changes. By following these guidelines, you can minimize the risk of encountering "401 Unauthorized" errors in your SharePoint Online environment.

Sources & References

Related Guides

Get new error code fixes by email

Product-specific solutions for Intune, Autopilot, SCCM and Entra ID — straight to your inbox. No spam.