Why is error: 400 occuring? 400 {'error': {'code': 'BadRequest', 'message': "'user@odata.bind' field is missing in the request.", 'innerError': ...

Michael Wilcox 26 Reputation points
2024-11-13T22:28:11.6833333+00:00
#  Here is a snippet of my code. Why is error 400 occuring:  ie  400  {'error': {'code': 'BadRequest', 'message': "'user@odata.bind' field is missing in the request.", 'innerError': {'date': '2024-11-13T22:20:30', 'request-id': '5675829f-5a', 'client-request-id': '567582a'}}} ? 


	# Create a new chat
    chat_payload = {
        "chatType": "oneOnOne",
        "members": [
            {
                "@odata.type": "#microsoft.graph.aadUserConversationMember",
                "roles": ["owner"],
                "user@odata.bind": "https://graph.microsoft.com/v1.0/me"
            },
            {
                "@odata.type": "#microsoft.graph.aadUserConversationMember",
                "roles": ["owner"],
                "user@odata.bind": f"https://graph.microsoft.com/v1.0/users{user_email}"
            }
        ]
    }
    # print("headers", headers, "chat payload",chat_payload)
    response = requests.post('https://graph.microsoft.com/v1.0/chats', headers=headers, json=chat_payload )
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,437 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. CarlZhao-MSFT 43,011 Reputation points
    2024-11-14T02:04:55.2666667+00:00

    Hi @Michael Wilcox

    Have you tried the Graph SDK?

    # Code snippets are only available for the latest version. Current version is 1.x
    from msgraph import GraphServiceClient
    from msgraph.generated.models.chat import Chat
    from msgraph.generated.models.chat_type import ChatType
    from msgraph.generated.models.conversation_member import ConversationMember
    from msgraph.generated.models.aad_user_conversation_member import AadUserConversationMember
    
    # To initialize your graph_client, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
    request_body = Chat(
    	chat_type = ChatType.OneOnOne,
    	members = [
    		AadUserConversationMember(
    			odata_type = "#microsoft.graph.aadUserConversationMember",
    			roles = [
    				"owner",
    			],
    			additional_data = {
    					"user@odata_bind" : "https://graph.microsoft.com/v1.0/users('8b081ef6-4792-4def-b2c9-c363a1bf41d5')",
    			}
    		),
    		AadUserConversationMember(
    			odata_type = "#microsoft.graph.aadUserConversationMember",
    			roles = [
    				"owner",
    			],
    			additional_data = {
    					"user@odata_bind" : "https://graph.microsoft.com/v1.0/users('82af01c5-f7cc-4a2e-a728-3a5df21afd9d')",
    			}
    		),
    	],
    )
    result = await graph_client.chats.post(request_body)
    

    Source: https://learn.microsoft.com/en-us/graph/api/chat-post?view=graph-rest-1.0&tabs=python#example-1-create-a-one-on-one-chat.

    Hope this helps.

    If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.


  2. Michael Wilcox 26 Reputation points
    2024-11-16T04:19:49.89+00:00

    I have a new error : DeviceCodeCredential.get_token failed: Authentication failed: AADSTS7000218: The request body must contain the following parameter: 'client_assertion' or 'client_secret'.

    import asyncio
    from msgraph import GraphServiceClient
    from msgraph.generated.models.chat import Chat
    from msgraph.generated.models.chat_type import ChatType
    from msgraph.generated.models.conversation_member import ConversationMember
    from msgraph.generated.models.aad_user_conversation_member import AadUserConversationMember
    from azure.identity import DeviceCodeCredential
    
    # Multi-tenant apps can use "common",
    # single-tenant apps must use the tenant ID from the Azure portal
    # tenant_id = 'common'
    tenant_id = "ccccccc"
    
    # Values from app registration
    client_id = "aaaaaaaaaaaaaaaaaaaa"
    client_secret = "bbbbbbbb"
    # To initialize your graph_client, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
    
    scopes = ['User.Read']
    
    # azure.identity
    credential = DeviceCodeCredential(
        tenant_id=tenant_id,
        client_id=client_id)
    
    graph_client = GraphServiceClient(credential, scopes)
    
    
    request_body = Chat(
    	chat_type = ChatType.OneOnOne,
    	members = [
    		AadUserConversationMember(
    			odata_type = "#microsoft.graph.aadUserConversationMember",
    			roles = [
    				"owner",
    			],
    			additional_data = {
    					"user@odata_bind" : "https://graph.microsoft.com/v1.0/users('zzzzzzzz')",
    			}
    		),
    		AadUserConversationMember(
    			odata_type = "#microsoft.graph.aadUserConversationMember",
    			roles = [
    				"owner",
    			],
    			additional_data = {
    					"user@odata_bind" : "https://graph.microsoft.com/v1.0/users('zzzzzzzz')",
    			}
    		),
    	],
    )
    # result = await graph_client.chats.post(request_body)
    # result = graph_client.chats.post(request_body)
    
    loop = asyncio.get_event_loop()
    result = loop.run_until_complete( graph_client.chats.post(request_body))
    loop.close()
    
    print (result)
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.