how to add a user using emailId to azure ad group using java spring boot application

50 views Asked by At

We have a requirement to add members to azureAdGroup using users email through java spring boot application, we are using microsoft graph api rest call to add members to the azureAdGroup

Trying to add members to azureAdGroup by using the members email through java spring boot application,we are using the below code to add the members using the graph API

DirectoryObject directoryObject = new DirectoryObject();
directoryObject.id = "[email protected]"; // members emailId

graphClient.groups(adGroupId).members().references()
   .buildRequest()
   .post(directoryObject);

But we are getting the below error

CoreHttpProvider[sendRequestInternal] - 404Graph service exception Error code: Request_BadRequest CoreHttpProvider[sendRequestInternal] - 404Error message: Invalid object identifier '[email protected]'. CoreHttpProvider[sendRequestInternal] - 404 CoreHttpProvider[sendRequestInternal] - 404POST https://graph.microsoft.com/v1.0/groups/sgdbhjd-sndeh3h3-dasdnj3k3-dwb23/members/$ref CoreHttpProvider[sendRequestInternal] - 404SdkVersion : graph-java/v5.0.0 CoreHttpProvider[sendRequestInternal] - 404SdkVersion : graph-java/v5.0.0

Could anyone please help how to add member to a azureAdGroup using members email through microsoft graphApi from java spring boot application

Could anyone please help how to add member to a azureAdGroup using user email through microsoft graphApi from java spring boot application

1

There are 1 answers

0
Naveen Sharma On

Note that: According to this MsDoc, to add users to the group you need to pass the id of the directoryObject not the UPN of the user.

For sample, I used Graph Explorer APIs to add the users to the group:

Passed the UPN of the user and got the error:

POST https://graph.microsoft.com/v1.0/groups/{group-id}/members/$ref
Content-type: application/json

{
  "@odata.id": "https://graph.microsoft.com/v1.0/directoryObjects/UPN"
}

enter image description here

To resolve the error, pass the ObjectID of the user not the UPN:

After passing the ObjectID, user got added successfully to the group:

enter image description here

enter image description here

Modify your code by passing the ObjectID of the user instead of UPN:

DirectoryObject directoryObject = new DirectoryObject();
directoryObject.id = "ObjectID"; // members ObjectID

graphClient.groups(adGroupId).members().references()
   .buildRequest()
   .post(directoryObject);