Help! Getting a 401 Unauthorized Error with PnP Online

May 6, 2025 2 answers
Asked by 7day_culture
Hey everyone! I'm trying to set up a connection to SharePoint using the PnP PowerShell module, and I'm authenticating with a certificate. Despite following all the steps correctly, I'm hitting a 401 Unauthorized error. Here's the command I'm running: ```powershell connect-pnponline -url $ConnectionURL -ClientId $ClientId -Tenant $TenantId -CertificatePath $CertPath -CertificatePassword (ConvertTo-SecureString $CertPassword -AsPlainText -Force) -Verbose ``` The verbose output shows that it's trying to connect using Entra ID App-Only with the certificate, but I can't seem to get past the Unauthorized status. I've also assigned all the necessary permissions and granted admin consent for both Microsoft Graph and SharePoint. Everything looks correct, but I just can't figure out what's wrong. Any advice would be greatly appreciated!

Answers

lively+discussion
Have you checked the output from `Get-PnPContext`? Sometimes it can give you clues about what's going wrong with the connection.

7day_culture: Not really, Iโ€™m not sure what to look for in the output, to be honest.

Reply to this answer
nearest exit
It sounds like you might need to double-check the SharePoint permissions. I faced a similar issue before. Make sure you run this command to grant the necessary site permissions: ```powershell Grant-PnPAzureADAppSitePermission -AppId -DisplayName -Permissions -Site ``` You can find more info in the [documentation here](https://pnp.github.io/powershell/cmdlets/Grant-PnPAzureADAppSitePermission.html).

transplant_D14: I tried that, but even after granting full control to the app, Iโ€™m still hitting a 401 error.

+9 degrees warmer: This is usually needed only if you're using Graph API permission for Sites.Selected, which doesnโ€™t apply in your case.

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions