ALM API - Provided Hosted AppOnly, app is not trusted
Category
- [x] Bug
Expected or Desired Behavior
ALM API should work correctly with provided hosted apps and app-only permissions, even for tenant scoped ones.
For example: AppManifest:
<?xml version="1.0" encoding="utf-8"?>
<!--Published:920014D5-5E5A-442D-934A-437E4FB0A4DA-->
<!--Created:cb85b80c-f585-40ff-8bfc-12ff4d0e34a9-->
<App xmlns="http://schemas.microsoft.com/sharepoint/2012/app/manifest" Name="HR_absences" ProductID="{80043127-46df-44c6-81bf-7fbe7f560aa5}" Version="1.0.0.1" SharePointMinVersion="16.0.0.0">
<Properties>
<Title>HR_absences</Title>
<StartPage>https://hr-2-absences.azurewebsites.net/?{StandardTokens}</StartPage>
</Properties>
<AppPrincipal>
<RemoteWebApplication ClientId="ae9cb07a-a97a-46d3-96b1-b5a6e053886e" />
</AppPrincipal>
<AppPermissionRequests AllowAppOnlyPolicy="true">
<AppPermissionRequest Scope="http://sharepoint/content/sitecollection/web" Right="Manage" />
<AppPermissionRequest Scope="http://sharepoint/content/sitecollection" Right="FullControl" />
<AppPermissionRequest Scope="http://sharepoint/social/tenant" Right="Read" />
</AppPermissionRequests>
</App>
Observed Behavior
App is installed on the site, however request for user profiles using app-only permissions fails:
var properties = new PeopleManager(_context).GetPropertiesFor(user.Login);
_context.Load(properties);
_context.ExecuteQueryRetry();
Exception:
Microsoft.SharePoint.Client.ServerUnauthorizedAccessException: Access denied. You do not have permission to perform this action or access this resource.
at Microsoft.SharePoint.Client.ClientRequest.ProcessResponseStream
at Microsoft.SharePoint.Client.ClientRequest.ProcessResponse
at Microsoft.SharePoint.Client.ClientRequest+<ExecuteQueryToServerAsync>d__4.MoveNext
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification
at Microsoft.SharePoint.Client.ClientRequest+<ExecuteQueryAsync>d__0.MoveNext
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification
at Microsoft.SharePoint.Client.ClientRuntimeContext+<ExecuteQueryAsync>d__0.MoveNext
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification
at Microsoft.SharePoint.Client.ClientContext+<ExecuteQueryAsync>d__4.MoveNext
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification
at Microsoft.SharePoint.Client.ClientContextExtensions+<ExecuteQueryImplementation>d__7.MoveNext
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification
at Microsoft.SharePoint.Client.ClientContextExtensions.ExecuteQueryRetry
Additionally, when navigating to app details, it will show: "Your tenant administrator has to approve this app."

Steps to Reproduce
- Prepare App file with App-Only permissions for
http://sharepoint/social/tenant, read access (or use attached file (HR_absences_xxx.app.zip) - Add into app catalog
- Run
Install-PnpAppto install app into site - Run code using app-only permissions to get someone user profile data
- Access denied
Submission Guidelines
Thanks for your contribution! Sharing is caring.
This exception information generally indicates that your permissions are not configured enough/inaccurate. Try to set the following permission in your project.

Hmm. Please note when I delete the app from site & both bins, then add the app manually (Add app -> select app -> request admin approval -> approve in app catalog -> add app -> select app -> install), everything is working correctly.
However I will give it a try what you have suggested and I will let you know about the results.
@Seiyasu
I have tried this:
And still received access denied. App is not set as trusted during Install-PnPApp. Could we have a fix for this?
This is actually unfortunately by design to avoid security issues. We are though missing a note in the documentation that this is not supported. Notice that even when you use the UI to approve the tenant level permissions, you will need to do this in the context of the tenant administrator. Only workaround is to grant the tenant level permission once in the tenant level for the specific app id with the needed permissions and then it can be installed on the sites.
If you'd like us to invest time on automating the tenant level permission request handling with an API, please submit an UserVoice entry at http://aka.ms/spdev-uservoice.
Issues that have been closed & had no follow-up activity for at least 7 days are automatically locked. Please refer to our wiki for more details, including how to remediate this action if you feel this was done prematurely or in error: Issue List: Our approach to locked issues