Publish requires a certain level of permission, which you must grant through the application previously registered in Azure AD.

The definition of permissions can be found here: https://learn.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azuread



Read/Write permission is strictly required.

The definition can be applied to all sites ( Sites.ReadWrite.All) or only to selected sites ( Sites.Selected)