.net 如何通过图形创建一对一团队聊天|图形API| C#

bcs8qyzn  于 2022-12-24  发布在  .NET
关注(0)|答案(1)|浏览(133)

我正在为我的组织开发一个控制台应用程序,它将获取一个电子邮件ID列表,并从某个电子邮件ID向他们发送一对一的聊天消息。
发送者和接收者-都来自同一个组织。
为了在我的本地计算机上测试这一点,我使用我的个人电子邮件(不同于组织电子邮件)在Azure AD中注册了一个应用,这些是权限。所有这些都是Application Permissions,我没有设置任何Delegated Permissions

这是创建聊天的代码。

private static async void CreateChat(string token)
        {
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
            GraphServiceClient graphClient = new GraphServiceClient(httpClient);

            var chat = new Chat
            {
                ChatType = ChatType.OneOnOne,
                Members = new ChatMembersCollectionPage()
                {
                    new AadUserConversationMember
                    {
                        Roles = new List<String>()
                        {
                            "owner"
                        },
                        AdditionalData = new Dictionary<string, object>()
                        {
                            {"user@odata.bind", "https://graph.microsoft.com/v1.0/users('$my_org_name@myorg.com')"}
                        }
                    },
                    new AadUserConversationMember
                    {
                        Roles = new List<String>()
                        {
                            "owner"
                        },
                        AdditionalData = new Dictionary<string, object>()
                        {
                            {"user@odata.bind", "https://graph.microsoft.com/v1.0/users('$receiver_org_name@myorg.com')"}
                        }
                    }
                }
            };

            var chatResult = graphClient.Chats
                .Request()
                .AddAsync(chat);

        }

我在chatResult中看到以下内容:

为了进行测试,我是否应该仅使用我的组织电子邮件在Azure AD中注册我的应用。
我在跟踪Example 3: Create a one-on-one chat using user principal name

k10s72fa

k10s72fa1#

这是我的代码,它的工作,但我用我的组织帐户,而不是我的私人。

///GetUser
GraphServiceClient graphServiceClient = this.GetAuthenticatedGraphClient(scopes);

  try
  {
    var result = await graphServiceClient.Users[userEmail].Request().GetAsync();
    return result;
  }
  catch (Exception ex)
  {
    if (raiseError == false)
      return null;
    else
      throw;

///Create Chat
  foreach (var curUser in userList)
    graphUsers.Add(curUser, base.GetUser(curUser, false, scopes));

  var chat = new Chat
  {
    ChatType = ChatType.OneOnOne,
    Members = new ChatMembersCollectionPage()
    {
      new AadUserConversationMember
      {
        Roles = new List<string>()
        {
          "owner"
        },
        AdditionalData = new Dictionary<string, object>()
        {
          { "user@odata.bind", "https://graph.microsoft.com/v1.0/users('" + userId + "')" }
        }
      },
      new AadUserConversationMember
      {
        Roles = new List<string>()
        {
          "owner"
        },
        AdditionalData = new Dictionary<string, object>()
        {
          { "user@odata.bind", "https://graph.microsoft.com/v1.0/users('"+ userList[0] +"')" }
        }
      }
    }
  };

  return graphClient.Chats.Request().AddAsync(chat);

相关问题