powershell: [BUG] Get-PnPApp Fails with access denied when connected using a SharePoint Addin Principal
On Mac OS I connect to a SharePoint site using a SharePoint Addin principal using the -ClientId and -ClientSecret parameters
The Addin principal was granted tenant scoped app-only permissions at the tenant SharePoint Admin centre
When I run the Get-PnPApp command I get an “Access denied. You do not have permission to perform this action or access this resource” error

On Mac OS if I connect to the same SharePoint site using the -PnPManagementShell method, running Get-PnPApp returns the list of solutions as expected.
On Mac OS if I connect to the same SharePoint site using the Credentials method, running Get-PnPApp returns the list of solutions as expected.
On Windows* if I connect to the same SharePoint site using the Addin ClientId and ClientSecret with the SharePointPnPPowerShellOnline module, running Get-PnPApp returns the list of solutions as expected.

Expected behavior
Connecting to a SharePoint site using a SharePoint Addin principal and running the ```Get-PnPApp` command should return the list of solutions
Actual behavior
An access denied error is returned
Steps to reproduce behavior
Create a SharePoint Addin registration
At the SharePoint Admin site grant the following permissions to the Addin principal
<AppPermissionRequests AllowAppOnlyPolicy="true">
<AppPermissionRequest Scope="http://sharepoint/content/tenant" Right="FullControl"/>
<AppPermissionRequest Scope="http://sharepoint/content/sitecollection" Right="FullControl"/>
<AppPermissionRequest Scope="http://sharepoint/search" Right="QueryAsUserIgnoreAppPrincipal"/>
<AppPermissionRequest Scope="http://sharepoint/social/tenant" Right="FullControl"/>
<AppPermissionRequest Scope="http://sharepoint/taxonomy" Right="Write"/>
</AppPermissionRequests>
Connect to a SharePoint site using the ClientId and ClientSecret for the addin registration
Run the Get-PnPApp command
What is the version of the Cmdlet module you are running?
PnP.PowerShell 0.3.4
Which operating system/environment are you running PnP PowerShell on?
- Windows
- Linux
- MacOS
- Azure Cloud Shell
- Azure Functions
- Other : please specify
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 15 (7 by maintainers)
Massive thanks @erwinvanhunen @veronicageek
Whatever you changed seems to have worked, tested this morning with 0.3.8 and
Get-PnPAppworked as expectedSo, the logic is as follows:
When you issue a connect-pnponline to login with ACS clientid/clientsecret we request from the AuthenticationManager in the PnP.Framework a clientcontext with a valid bearer token. That token is used in every subsequent CSOM request by overriding the ExecutingWebRequest event and adding the token to the header collection. This works for you as you see requests going over the wire to the ProcessQuery endpoint (that’s the CSOM endpoint) with the correct bearer token added. However, the AppManager in the PnP.Framework (which is used behind Get-PnPApp) has to use REST, so we have to acquire the accesstoken separately.
For that to work we first check how the original authentication is done. If it’s using OAuth, we acquire the token from the MSAL cache. If not we we are temporarily adding an additional ExecutingWebRequest handler to the client context you’re using. We then issue a non-modifying CSOM request, and get the Authentication header from the request. We then remove the newly added event handler from the client context as we don’t need it anymore. That access token is returned to the AppManager and added to every request made by it.
This all works flawless on my end… in both Windows and Linux…
However, we I did a relatively heavy refactoring of the AccessToken acquisition part in the PnP.Framework today for another issue, so it would be great if you could give it a try with the nightly build of PnP PowerShell that is available tomorrow (0.3.8)
Thanks for your help @erwinvanhunen & @veronicageek this type of issue is always tricky to track down 🙂
I’ll try and spin up a windows VM with PSCore to try and replicate there, if you can point me in the right direction code-files wise happy to look around there too
@erwinvanhunen - There’s apparently Fiddler Everywhere for MacOS, Linux & Windows, so I’ll dig more into those 2 tests.