New-PnPTenantSite: The remote server returned an error: (401) Unauthorized.

This blog is a continuation of my previous article, "Automating SharePoint Site Provisioning with PnP Scripts: Overcoming Authentication Challenges (App-based authentication)".

While trying to connect use the following command.

Connect-PnPOnline -Url <URL> -ClientId <ClientID> -ClientSecret <ClientSecret>

I encountered the error: "New-PnPTenantSite: The remote server returned an error: (401) Unauthorized" for all new SharePoint Tenants.

This was quite perplexing, and after extensive research, I discovered that the issue was due to custom app authentication being enabled. To resolve this, we need to disable custom app authentication using PowerShell. Below are the commands required to execute this.

Steps to Disable Custom App Authentication
 

1. Connect to SharePoint Online Service

Connect-SPOService

2. Disable Custom App Authentication

Set-SPOTenant -DisableCustomAppAuthentication $false

Executing these commands resolved the "401 Unauthorized" error, allowing the PnP scripts to run as scheduled. By following these steps, you can troubleshoot and fix similar authentication issues in your SharePoint automation processes.

Conclusion

Disabling custom app authentication resolved the "401 Unauthorized" error, allowing the PnP scripts to run as scheduled. By following these steps, you can set up SharePoint Add-ins, grant necessary permissions, and troubleshoot common authentication issues in your automation workflow.

Happy development!

Feel free to connect with me on LinkedIn.