Johnny
Johnny

Reputation: 65

How can I get the Json Response of create chat from Microsoft Graph Api

I'm trying to get the response of create chat function with Microsoft Graph Api in my c# Application and show it in a textbox, to let my c# system be able to sent message with the chatID. I was be able to get the response of getallchat with following the steps in How can I parse the JSON response from Microsoft Graph Api List Chat into the textbox, bt I was nt unable to use the following steps to get the response of create chat any ideas?Documentation of create chat https://learn.microsoft.com/en-us/graph/api/chat-post?view=graph-rest-1.0&tabs=http

Coding Part of create chat

                 private async void button5_Click(object sender, EventArgs e)
    {
        var scopes = new[] { "Directory.Read.All", "Directory.ReadWrite.All", "User.Read", "User.Read.All", "User.ReadBasic.All", "User.ReadWrite", "Chat.Create", "Chat.ReadWrite" };
        {





            // Multi-tenant apps can use "common",
            // single-tenant apps must use the tenant ID from the Azure portal
            var tenantId = "5xxxxxxx3-3xxx6a-xxx1-9xxxc-exxxxxxxxx0";



            // Value from app registration
            var clientId = "3xxxx04-5c92-42xxx-8500-6xxxxxxxaf";



            // using Azure.Identity;
            var options = new TokenCredentialOptions
            {
                AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
            };



            var userName = "[email protected]";
            var password = "Aaxxxxxxxxxxx@";



            // https://learn.microsoft.com/dotnet/api/azure.identity.usernamepasswordcredential
            var userNamePasswordCredential = new UsernamePasswordCredential(
                userName, password, tenantId, clientId, options);
            GraphServiceClient graphClient = new GraphServiceClient(userNamePasswordCredential, scopes);



            var chat = new Chat
            {
                ChatType = ChatType.OneOnOne,
                Members = new ChatMembersCollectionPage()
{
    new AadUserConversationMember
    {
        Roles = new List<String>()
        {
            "owner"
        },
        AdditionalData = new Dictionary<string, object>()
        {
            {"[email protected]", "https://graph.microsoft.com/v1.0/users[comboBox1.Text]"}
        }
    },
    new AadUserConversationMember
    {
        Roles = new List<String>()
        {
            "owner"
        },
        AdditionalData = new Dictionary<string, object>()
        {
            {"[email protected]", "https://graph.microsoft.com/v1.0/users/[comboBox2.Text]"}
        }
    }
}
            };
           
            await graphClient.Chats
                .Request()
                .AddAsync(chat);




        }
    }

Error Part

Upvotes: 0

Views: 436

Answers (1)

Tiny Wang
Tiny Wang

Reputation: 16066

here's the sample from the api document.

pls try to give the application api permission and try code below.

enter image description here enter image description here

using Microsoft.Graph;
using Azure.Identity;

var scopes = new[] { "https://graph.microsoft.com/.default" };
var tenantId = "tenant_name.onmicrosoft.com";
var clientId = "aad_app_id";
var clientSecret = "client_secret";
var clientSecretCredential = new ClientSecretCredential(
                tenantId, clientId, clientSecret);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
var chat = new Chat
{
    ChatType = ChatType.OneOnOne,
    Members = new ChatMembersCollectionPage()
    {
        new AadUserConversationMember
        {
            Roles = new List<String>()
            {
                "owner"
            },
            AdditionalData = new Dictionary<string, object>()
            {
                {"[email protected]", "https://graph.microsoft.com/v1.0/users('8b081ef6-4792-4def-b2c9-c363a1bf41d5')"}
            }
        },
        new AadUserConversationMember
        {
            Roles = new List<String>()
            {
                "owner"
            },
            AdditionalData = new Dictionary<string, object>()
            {
                {"[email protected]", "https://graph.microsoft.com/v1.0/users('82af01c5-f7cc-4a2e-a728-3a5df21afd9d')"}
            }
        }
    }
};

var res = await graphClient.Chats.Request().AddAsync(chat);

Upvotes: 1

Related Questions