add member in Outlook using Java Microsoft graph SDK

我的梦境 提交于 2019-12-08 07:18:52

问题


I'm trying to add user to a group in outlook using Microsoft Graph API in java. I've referred developer's guide : https://developer.microsoft.com/en-us/graph/docs/api-reference/beta/api/group_post_members for adding user.

However to achieve this I'm using Microsoft graph java sdk and I need to add the json object with member Id in the group in java, like

POST https://graph.microsoft.com/v1.0/groups/{id}/members/$ref
Content-type: application/json
Content-length: 30
{
    "@odata.id": "https://graph.microsoft.com/v1.0/users/{id}"
}

Please let me know how I can add the json object in the body of the request in java.

My code looks as below:

public void addMemberToGroup(String groupId,String userId) {

            Group group =  mGraphServiceClient
                                              .groups(groupId)
                                              .buildRequest()
                                              .get();

                JsonObject payload1 = new JsonObject();
                        IJsonBackedObject requestBody = new ReferenceRequestBody("https://graph.microsoft.com/v1.0/users/78276c08-9802-4108-8b20-d70cff6666e5");

                        mGraphServiceClient
                        .groups(groupId)
                        .members(userId)
                        .buildRequest()
                        .post(user,requestBody);

}

With this I'm getting error as below :

SEVERE: Throwable detail: com.microsoft.graph.http.GraphServiceException: Error code: BadRequest Error message: Write requests are only supported on contained entities

POST https://graph.microsoft.com/v1.0/groups/5877490c-54fe-45fb-b288-b5d0f6902058/members/78276c08-9802-4108-8b20-d70cff6666e5 SdkVersion : graph-java-v0.2.0 Authorization : Bearer eyJ0eXAiOiJKV1QiLCJub25jZSI[...] {"@odata.id":"https://graph.microsoft.com/v1.0/use[...]

400 : Bad Request [...]

Please let me know how I could resolve this.


回答1:


it should be like this:

User user = new User();
user.id=userId;

mGraphServiceClient.groups(groupId).members().references().buildRequest().post(user);

or

DirectoryObject directoryObject = new DirectoryObject();
directoryObject.id = userId;  

mGraphServiceClient.groups(groupId).members().references().buildRequest().post(directoryObject);


来源:https://stackoverflow.com/questions/53140576/add-member-in-outlook-using-java-microsoft-graph-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!