Let us add/ update the API Permissions for the App
More on registering App – https://nishantrana.me/2024/07/30/calling-sharepoint-online-api-using-azure-ad-app-only-permissions-using-certificate-auth/

Here we have selected the Sites.Selected permission.

Also granted the Admin consent.


Now if we run our code we will get the 403 Forbidden error.

This is because we have selected the Sites.Selected permission, but have not specified the site and the permission.
For that, we can use either Graph API or PnP PowerShell.
Let us use PnP PowerShell here.
Grant-PnPAzureADAppSitePermission -AppId “{app-id}” -DisplayName “{app-name}” -Site “https://{tenant}.sharepoint.com/sites/{site-name}” -Permissions “Write”
Grant-PnPAzureADAppSitePermission -AppId “d7eaeeb7-ef0a-474d-9b94-567013576c14” -DisplayName “SharePointApp” -Site “https://w72tk.sharepoint.com/sites/MyTeamSite” -Permissions “Write”

This time as expected we do not get the Forbidden error.

The other Permissions that we can specify are – Read, Manage, FullControl.
Get all the details here –https://pnp.github.io/powershell/cmdlets/Grant-PnPAzureADAppSitePermission.html
Hope it helps..





