SharePoint Online: "The remote server returned an error (401) Unauthorized" Error in PowerShell

Problem: "The remote server returned an error (401) Unauthorized." Error in SharePoint Online
The remote server returned an error: (401) Unauthorized.

Troubleshooting Checklist:
Here is the various root causes and solutions for this issue:

1. Is your URL Correct?
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Set Variables
$SiteURL = "http://crescent.sharepoint.com/sites/marketing" 
 
#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
 
#Get the Web
$Web=$Ctx.Web
$Ctx.Load($Web)
$Ctx.ExecuteQuery()
You'll get the same error "Connect-SPOService : The remote server returned an error: (401) Unauthorized." if you try to connect using Connect-SPOService cmdlet.
Connect-SPOService -Url "http://crescent-admin.sharepoint.com" -Credential (Get-Credential)
Here in this script, the URL was incorrect! instead of "https://tenant.sharepoint.com", it was "http://tenant.sharepoint.com". The SharePoint Online URL must start with HTTPS always!

2. Do you have Permissions to the site?
If you don't have permission to access the target site through browser, you can't access it from PowerShell too (absolutely!). So, check if you have sufficient permission and able to open the site in web browser.

3. Legacy authentication protocol is enabled?
Check if Legacy authentication protocol is enabled in your tenant.
Get-SOPTenant -LegacyAuthProtocolsEnabled
If not, enable it with:
Set-SOPTenant -LegacyAuthProtocolsEnabled $True 
SharePoint Online: "The remote server returned an error (401) Unauthorized" Error in PowerShell SharePoint Online: "The remote server returned an error (401) Unauthorized" Error in PowerShell Reviewed by Salaudeen Rajack on June 05, 2019 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.