0

I'm trying to run my Powershell PnP script using ClientId and ClientSecret instead of UseWebLogin. I already created an Azure Application ID using this tutorial (but skipping the certificate part for the moment):

https://yunusemrearac.medium.com/connecting-to-sharepoint-online-using-azure-ad-application-id-via-powershell-49191054ae40

This is my code:

#Install-Module SharePointPnPPowerShellOnline
#Config Variables
$SiteURL = "https://myname.sharepoint.com/"

    Try {
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -ClientId "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" -ClientSecret "Sdf9Q~9pZErHkefW0Zt77IryFh-6vk_vI6B-Fch2"

Get-PnPsite

Disconnect-PnPOnline
 } 
 catch {  
     write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
 } 

I get this error:

Get-PnPsite : Cannot contact site at the specified URL https://myname.sharepoint.com/. The app principal does not exist. At line:9 char:1

Using the UseWebLogin all is working.

What did I do wrong or miss in creating the Azure Application?

3
  • Don't you miss the -Tenant argument? For example: -Tenant 'myname.onmicrosoft.com' Commented Jun 27, 2024 at 5:01
  • No, -Tenant parameter does not exist when using -ClientSecret but it works if I use -Thumbprint, and at this point I wonder if -ClientSecret is usable here. Commented Jun 27, 2024 at 14:18
  • So maybe you're using AppId instead of ClientId... That's why you have the error The app principal does not exist. Commented Jun 27, 2024 at 18:28

1 Answer 1

0

If your code works with a cert, but does not work with a secret - I'd suspect a bug somewhere. I use Visual Studio Code, PowerShell 7 and PnP.PowerShell latest version (2.5 as of today).

For app (unattended) access:

  • if you are administering entire SharePoint - you'd have Sites.FullControl.All for both - Graph API and SharePoint API for full access to all sites.
  • if you need to work with one site only - use Sites.Selected permissions

It works just perfectly:

Connect-PnPOnline -Url $siteUrl -ClientId $clientID -ClientSecret $clientSc

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.