Hi @Sumesh Chandran ,
Thanks for reaching out.
The error you are getting is 401 which is an Unauthorized error. The access token you are getting does not have valid permissions to create the user.
Did you tried to decode the token using jwt.ms to check the valid claims.
As mentioned in your code, you are using client credential flow to get the access token which is usually called for daemon applications.
Make sure you are passing application permissions User.ReadWrite.All, Directory.ReadWrite.All while registering the application and getting roles claim in your JWT token.
Also, you need to make sure you have a token with the aud claim of https://graph.microsoft.com or 00000003-0000-0000-c000-000000000000. If you are getting https://graph.windows.net a.k.a. 00000002-0000-0000-c000-000000000000, that means the token is for AAD Graph API and not for Microsoft Graph.
Hope this will help.
Thanks,
Shweta
--------------------------------------
Please remember to "Accept Answer" if answer helped you.