How to see Graph API error response

Mateusz Krawczyk 21 Reputation points
2022-02-17T17:32:47.17+00:00

I am trying to make use of Graph API. On the beginning I was trying to get simply user data, but with below code I was unable: some exception was thrown inside library of async methods (AsyncStateMachineBox::ExecutionContextCallback).

var user = await GetAuthenticatedGraphClient().Me.Request().Select(u => new {
                u.DisplayName,
                u.Mail,
                u.UserPrincipalName
            }).GetAsync();

When I sent the same request via cmd and curl I saw response that I don't have permissions. Once they were granted above code started to work. Looks like Graph is responding with an error message and above code didn't check for error but just tries to parse the request to read user's data.
I granted other required permissions and started to make more complicated requests. Sometimes I'm getting the same exception; I think we should be able to read what error the Graph API sends to the application? Surrounding request with try{}catch block didn't catch the exception here (async method?).

How can I know what's wrong with my code, with the request? There is even no data I can provide: what can be helpful for you to help me?
Sending above request from cmd (curl) I can see the response and I know I cannot make this request with application permission. But For other requests - how can I know what's wrong (what's the error message received)?

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,586 questions
0 comments No comments
{count} votes

Accepted answer
  1. CarlZhao-MSFT 36,896 Reputation points
    2022-02-18T08:10:06.357+00:00

    Hi @Mateusz Krawczyk

    I tested your code snippet with VS 2022 and it responds perfectly to my error message, it's telling me that my authorization code has expired. So the reason why you are not getting an error message response may be related to the IDE you are using.

    175726-image.png


    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.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful