@Ori Shlomo, Thanks for posting in Q&A. For the error message, it shows forbidden which seems to be with permission issue. But we add DeviceManagementManagedDevices.ReadWrite.All permission but still with the error. Please enter the token we use when get error here to analyze if it has enough permission. And get screen shot of the result. As a note, please hide the sensitive information like tenant information username and etc when upload the screen shot.
Meanwhile, if seems sometimes work but sometimes not. Could you confirm if we use the same user account always? if not, please check if the affected user account has Intune license assigned.
In addition, please also let us know which flow we choose:
https://learn.microsoft.com/en-us/entra/identity-platform/v2-oauth2-auth-code-flow
Please check the above information and if there's any update, feel free to let us know.
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.