So you have started using Graph API through PowerShell and gotten pretty great at it. Then one day while writing a script and doing stuff you swear have worked before, suddenly fail. First of all, maybe you are running it in another tenant and have created a new application with other permission sets. You go through the docs pages to see that the requests you are running have the correct permissions. You keep getting errors.
It can be a 404 Not Found, 401 Unauthorized or anything else. You really don’t get that much information on why you are getting those errors. Whats underlying cause for this error?
I this case I am running a POST request to create a group.
Thanks to Lee Ford for showing me this handy little way of debugging Graph API errors:
When putting your request inside a try/catch block you are able to read out the real reason behind why it’s failing. So for my 404 error, I was able to get the following:
Code field says Request_ResourceNotFound. If you look close in the message field you see something resembles an object ID. In my request body for creating the group, I have specified an owner of the group. When I look closer, there is a typo in the object ID and therefore I’m getting this error. Graph cant find the user it’s trying to add since it does not exist.
Another example, if I mess up the JSON request body. Say I forget to specify an owner of the group, which is required when you run the request with application permissions.
Now I get Error 400 Bad Request:
So let’s look at the ResponseBody for this error:
Hope this eases your debugging and takes away a lot of frustration when working with the Graph API.