简体   繁体   中英

Adding User to Group using Graph API B2C

I am using Azure AD B2C to create users. After a user is created I would like to add them to a Group

Using Postman I have made the following request (with access token in the header). Note: I have managed to successfully create a group using the Api.

POST https://graph.windows.net/{azure-tenant}/groups/{group-objectId/members/$ref?api-version=1.6

With Body:

{
    "@odata.id": "https://graph.windows.net/{azure-tenant}/directoryObjects/{user-objectId}"
}

Which is what the documentation specifies. No matter the body that is sent I get the following error

{
    "odata.error": {
        "code": "Request_BadRequest",
        "message": {
            "lang": "en",
            "value": "The request URI is not valid. Since the segment 'members' refers to a collection, this must be the last segment in the request URI. All intermediate segments must refer to a single resource."
        },
        "requestId": "48cf65f3-20ba-411e-8121-f7ea54252f3a",
        "date": "2019-05-27T06:09:25"
    }
}

I tried removing the /$ref

POST https://graph.windows.net/{azure-tenant}/groups/{group-objectId/members?api-version=1.6

Body:

{
    "@odata.id": "https://graph.windows.net/{azure-tenant}/directoryObjects/{user-objectId}"
}

as well as

{
    "@odata.id": "https://graph.windows.net/{azure-tenant}/users/{user-objectId}"
}

Now the error that gets returned now is:

{
    "odata.error": {
        "code": "Request_BadRequest",
        "message": {
            "lang": "en",
            "value": "Unsupported resource type 'DirectoryObject' for operation 'Create'."
        },
        "requestId": "2c36cc6d-383c-44f8-8609-2ac3e3efc862",
        "date": "2019-05-27T06:15:26"
    }
}

The documentation that I have been using https://docs.microsoft.com/en-us/graph/api/group-post-members?view=graph-rest-1.0&tabs=javascript

You are using graph.windows.net endpoint, so you should refer to azure ad graph api .

POST https://graph.windows.net/myorganization/groups/b4bda672-1fba-4711-8fb1-5383c40b2c14/$links/members?api-version=1.6

{
  "url": "https://graph.windows.net/myorganization/directoryObjects/3eb6055a-baeb-44d4-a1ea-2fee86d8891b"
}

The document you referred to is microsoft graph api. The endpoint should be https:// graph.microsoft.com .

You can use Microsoft Graph explorer to call these apis.

在此输入图像描述

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM