Hello Florian Weidinger,
Thank you for reaching out to Microsoft Support!
From your problem description, it is clear that you are using Auth code flow when obtaining tokens.
To get a token using Auth code flow, you need to perform an interactive user login to get authorization_code first, and you have no problems here.
Then when you use the value of code to get the access token, you have a problem, the token is invalid, and the details of the request token are not shown in your code snippet, so we recommend that you refer to the request for the token in this document, as follows:
// Line breaks for legibility only
POST /{tenant}/oauth2/v2.0/token HTTP/1.1
Host: https://login.microsoftonline.com
Content-Type: application/x-www-form-urlencoded
client_id=11112222-bbbb-3333-cccc-4444dddd5555
&scope=https%3A%2F%2Fgraph.microsoft.com%2Fmail.read
&code=OAAABAAAAiL9Kn2Z27UubvWFPbm0gLWQJVzCTE9UkP3pSx1aXxUjq3n8b2JRLk4OxVXr...
&redirect_uri=http%3A%2F%2Flocalhost%2Fmyapp%2F
&grant_type=authorization_code
&code_verifier=ThisIsntRandomButItNeedsToBe43CharactersLong
&client_secret=sampleCredentia1s // NOTE: Only required for web apps. This secret needs to be URL-Encoded.
Please refer to the code to obtain the token. At the same time, after obtaining the token, please parse the token in jwt.ms to check whether the token has the permission you need.
Hope this helps.
If the answer is helpful, please click Accept Answer and kindly upvote it. If you have any further questions about this answer, please click Comment.