I am calling the graph api with the token returned from the app to create a user in the tenant. This api totally works fine when calling through postman but not when calling through the mobile app which is created using Flutter.
Here is my request to get the access token through my mobile app. The below code works as expected and returns an access token.
final response = await http.post(
Uri.parse(
'https://login.microsoft.com/tenant-id/oauth2/token'),
headers: {
"Content-Type":
"application/x-www-form-urlencoded",
},
body: {
"grant_type": "client_credentials",
"client_id":
"client-id",
"client_secret":
"client-secret",
"resource": "https://graph.microsoft.com"
},
);
Now I use the access token to create a user, this returns 401 and does not create a user.
String url = "https://graph.microsoft.com/v1.0/users";
Map<String, String> headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization':
'Bearer $token'
};
final body = jsonEncode({
"accountEnabled": true,
"city": "Seattle",
"country": "United States",
"department": "Sales & Marketing",
"displayName": "Melissa Darrow",
"givenName": "Melissa",
"jobTitle": "Marketing Director",
"mailNickname": "MelissaD",
"passwordPolicies": "DisablePasswordExpiration",
"passwordProfile": {
"password": "82510f31-1c89-d103-73c8-9fbedda45dcc",
"forceChangePasswordNextSignIn": false
},
"officeLocation": "131/1105",
"postalCode": "98052",
"preferredLanguage": "en-US",
"state": "WA",
"streetAddress": "9256 Towne Center Dr., Suite 400",
"surname": "Darrow",
"mobilePhone": "+1 206 555 0110",
"usageLocation": "US",
"userPrincipalName": "MelissaD@myorg.onmicrosoft.com"
});
final response = await http.post(Uri.parse(url),
headers: headers, body: body);
print(response.statusCode);
Please advise!