Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add members using patch method does not add users to group #611

Open
marvin-garcia opened this issue Feb 29, 2024 · 5 comments
Open

Add members using patch method does not add users to group #611

marvin-garcia opened this issue Feb 29, 2024 · 5 comments
Assignees
Labels
bug Something isn't working type:documentation General documentation request or project documentation update

Comments

@marvin-garcia
Copy link

marvin-garcia commented Feb 29, 2024

Title
Add members using patch method does not add users to group.

Environment
Python Version: 3.10.12
msgraph-sdk version: 1.1.0
OS: Ubuntu 22.04

Stack trace (if available) Screenshot or formatted copy and paste of your stack trace.
N/A

Describe the bug A clear and concise description of what the bug is.

I have the following methods to add a single and multiple users to a group in my AAD B2C tenant. The post method works, and the user is added to the AAD group. The patch method does not work, it returns None and does not raise any exception. I tried the same payload with my user Ids using the MS Graph explorer and users are added successfully. The service principal has full Group, GroupMember and User permissions in the MS Graph API.

async def add_group_member(group_id: str, user_id: str) -> bool:
    """Adds a single user to a group."""
    try:
        if not user_id:
            return False
        
        request_body = ReferenceCreate(odata_id = f'https://graph.microsoft.com/v1.0/directoryObjects/{user_id}')
        await GRAPH_CLIENT.groups.by_group_id(group_id).members.ref.post(request_body)

        return True
    except Exception as e:
        raise e

async def add_group_members(group_id: str, user_ids: List[str]) -> bool:
    """Adds users to a group."""
    try:
        if not user_ids:
            return False
        elif len(user_ids) > 20:
            raise Exception("You can only add 20 users at a time.")
        elif len(user_ids) == 1:
            return await self.add_group_member(group_id, user_ids[0])

        request_body = Group(
            additional_data = {
                "members@odata_bind" : [f'https://graph.microsoft.com/v1.0/directoryObjects/{user_id}' for user_id in user_ids]
            }
        )
        await GRAPH_CLIENT.groups.by_group_id(group_id).patch(request_body)

        return True
    except Exception as e:
        raise e

To Reproduce Steps to reproduce the behavior:
GRAPH_CLIENT = ClientSecretCredentials(...)
add_group_members(group_id, user_ids)

Expected behavior A clear and concise description of what you expected to happen.
Multiple groups added to the group in a single patch request.

Screenshots If applicable, add screenshots to help explain your problem.
N/A

Additional context Add any other context about the problem here.
N/A

@e88z4
Copy link

e88z4 commented Mar 20, 2024

I have similar issue. I got None as the response but the group is not added.

@vicecarloans
Copy link

Hey all, I found out that this code works to add user to group

# Add user to group
body = ReferenceCreate(
    odata_id=f"https://graph.microsoft.com/v1.0/users/{azure_user_id}"
)
response = await self.graph_client.groups.by_group_id(
    group_id
).members.ref.post(body=body)

@shemogumbe shemogumbe added the bug Something isn't working label Apr 3, 2024
@LuchoPeres
Copy link

Hey all, I found out that this code works to add user to group

# Add user to group
body = ReferenceCreate(
    odata_id=f"https://graph.microsoft.com/v1.0/users/{azure_user_id}"
)
response = await self.graph_client.groups.by_group_id(
    group_id
).members.ref.post(body=body)

yeah, but keep in mind that this is doing a POST request for each user added, the PATCH is useful to add multiple users at once. it is working on the API using the request directly but through the SDK is failing.

@LuchoPeres
Copy link

I managed to solve the issue by adjusting the body as shown below.

request_body = Group (
    additional_data = {
        "members@odata.bind": [
            f"https://graph.microsoft.com/v1.0/directoryObjects/{id}",
        ]
    } 
) 

Although it looked like a bug at first, the problem was actually with the properties. The distinction is in using 'odata.bind' as opposed to 'odata_bind'. I do believe that this error should publish a warning though. This inconsistency can also be seen in multiple sections of the official Microsoft Graph API documentation.

@andrueastman
Copy link
Member

This is an issue with the snippet generator which needs to be fixed here to avoid snake casing the key values present in the additional_data bag.

@andrueastman andrueastman added the type:documentation General documentation request or project documentation update label May 8, 2024
@shemogumbe shemogumbe self-assigned this Aug 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working type:documentation General documentation request or project documentation update
Projects
None yet
Development

No branches or pull requests

6 participants