Troubleshoot and Resolve: PowerShell Error (401) Unauthorized When Executing Commands to Enable API Permissions for Site Pages Restoration

Troubleshoot and Resolve: PowerShell Error (401) Unauthorized When Executing Commands to Enable API Permissions for Site Pages Restoration

KB ID: 310007
Error:
Get-PnPTenantSite : The remote server returned an error: (401) Unauthorized.
Problem Description:
When using the `Get-PnPTenantSite` command in PowerShell, an error is encountered, indicating unauthorized access (HTTP error 401).
Solution:
Step 1: Validate Microsoft 365 License
Verify that the admin user, for whom SharePoint permissions are being enabled, has a valid Microsoft 365 license.
Step 2: Check Site URL Accessibility
  1. Ensure that the Site URL, for which permissions are being added, is accessible through a web browser.
  2. If not accessible, grant the necessary permissions for the admin user to access the Site URL.
Step 3: Verify DisableCustomAppAuthentication Value
Check the `DisableCustomAppAuthentication` value from the SharePoint Online organization properties for the admin user.
  1. Execute the following PowerShell commands:
Install-Module Microsoft.Online.SharePoint.PowerShell
  1.    In the pop-up window, log in with the admin credentials associated with the SharePoint permissions.
Get-SPOTenant
  1. If the `DisableCustomAppAuthentication` value is set to `true`, execute the following command to set it to `false`:
Set-SPOTenant -DisableCustomAppAuthentication $false
Note: Ensure that you have the necessary permissions to make changes at the SharePoint Online organization level.