繁体   English   中英

使用 c# 应用系统向团队聊天发送消息

[英]Sending message to Team chat using c# application system

目前我正在使用 Microsoft Graph API beta 的方式,您可以在聊天中发送消息来编写应用程序,但我遇到这些错误找不到类型或名称空间 ChatMessage名称 authProvider 在当前上下文中不存在await 运算符只能在 async 修饰符中使用,并将其返回类型更改为 tásk并且Microsoft.GraphGraphServiceClient 不包含 Chat 的定义,并且没有可以找到接受 Microsoft.Graph.Graph.ServiceClient 类型的第一个参数的扩展方法 Chats . 谁能帮我解决它。

非常感谢。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Graph;

namespace w
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        
            GraphServiceClient graphClient = new GraphServiceClient(**authProvider**);

            var chatMessage = new **ChatMessage**
            {
                Body = new ItemBody
                {
                    Content = "Hello world"
                }
            };

            *await* graphClient.**Chats**["19:056680e9-868a-49a2-bf96-2ba2d2c39b5d_76ff09a8-7650-426d-843b-3d1104128f25@unq.gbl.spaces"].Messages
      .Request()
      .AddAsync(chatMessage);
        }
    }
}

}

根据 文档使用using Microsoft.Graph.Beta; 而不是using Microsoft.Graph; .

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Graph.Beta;

确实,我们最好不要使用测试版 api 但实际上这个 api有测试版,所以使用测试版应该不会导致异常。

根据您的错误信息, The name authProvider does not exist in the current context ,所以您似乎误解了authProvider中提到的authProvider。 有一个超链接

在此处输入图像描述

关于authProvider ,即包含授权信息的 object 。 await* graphClient.**Chats**["19..... is calling the ms graph api, and we need the access token in the request header to call the api right? But now since you are using the graph SDK,所以你只需要准备authProvider。

AuthProvider 是 Azure AD 中包含的东西,Azure AD 为您提供两种流程,一种是用户先登录才能获得授权,因此您必须在这种情况下提供一个登录页面,例如Authorization code provider 另一个用于应用程序,因此没有用户登录,您必须创建一个 azure 广告应用程序,然后获取应用程序客户端密码以在您自己的代码中验证广告应用程序,这称为Client credentials provider 但是此 api 不支持Client credentials provider

因此,您可以按照本节创建 Auth 代码提供程序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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