图 API:向频道发送消息失败,因为被禁止(线程未标记为导入)

Graph API: sending message to a channel fails as Forbidden (Thread is not marked for import)

我需要使用 Graph API 将聊天消息发送到 MS Teams 频道,documented here

我从拥有所需权限的应用程序注册中检索了一个令牌:

权限有管理员同意

这是获取令牌的代码 C#

var scopes = new[] {
    "https://graph.microsoft.com/.default",
};
var tenantId = "602*********************************";
var clientId = "634*********************************";
var clientSecret = "DfV*************************************";
var options = new TokenCredentialOptions
{
    AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};
var clientSecretCredential = new ClientSecretCredential(
    tenantId, clientId, clientSecret, options);

var token = clientSecretCredential.GetToken(new TokenRequestContext(scopes)).Token;

我使用 Postman 来更好地检查响应。这是 POST 请求的 URL:

https://graph.microsoft.com/v1.0/teams/991*********************************/channels/19%3a9*********************************%40thread.tacv2/messages

Authorization header 由 C# 代码获取的 token 设置,payload 如下:

{
  "createdDateTime": "2022-03-07T17:47:11.7429830Z",
  "from": {
    "user": {
      "id": "2a7*********************************",
      "displayName": "User display name",
      "userIdentityType": "aadUser"
    }
  },
  "body": {
    "contentType": "html",
    "content": "Hello Teams!"
  }
}

此 REST API 调用在以租户用户身份登录时与 Graph Explorer 完美配合(仅使用 body 属性)。使用应用程序注册令牌和其他属性,我得到以下响应和 HTTP 状态代码 403 Forbidden:

{
    "error": {
        "code": "Forbidden",
        "message": "{\"errorCode\":209,\"message\":\"{\r\n  \\"subCode\\": \\"MessageWritesBlocked\\",\r\n  \\"details\\": \\"Thread is not marked for import\\",\r\n  \\"errorCode\\": null,\r\n  \\"errorSubCode\\": null\r\n}\"}",
        "innerError": {
            "date": "2022-03-07T17:47:34",
            "request-id": "e0ab5c7b-b64b-4c44-81e3-cc91b344403c",
            "client-request-id": "e0ab5c7b-b64b-4c44-81e3-cc91b344403c"
        }
    }
}

我不明白如何满足Thread is not marked for import

对于如何在此次通话中取得成功的任何帮助,我将不胜感激。如果不可能:使用带有应用程序注册令牌的图表 API 发送消息的任何其他方式。

应用程序权限仅支持迁移。

请按照以下步骤操作。

1.在迁移状态下创建新团队。

POST https://graph.microsoft.com/v1.0/teams

Content-Type: application/json
{
  "@microsoft.graph.teamCreationMode": "migration",
  "template@odata.bind": "https://graph.microsoft.com/v1.0/teamsTemplates('standard')",
  "displayName": "My Sample Team",
  "description": "My Sample Team’s Description",
  "createdDateTime": "2020-03-14T11:22:17.043Z"
}

2。在迁移状态中创建新频道

POST https://graph.microsoft.com/v1.0/teams/{team-id}/channels

Content-Type: application/json
{
  "@microsoft.graph.channelCreationMode": "migration",
  "displayName": "Architecture Discussion",
  "description": "This channel is where we debate all future architecture plans",
  "membershipType": "standard",
  "createdDateTime": "2020-03-14T11:22:17.047Z"
}

3。执行导入消息的图形查询

POST https://graph.microsoft.com/v1.0/teams/team-id/channels/channel-id/messages

{
   "createdDateTime":"2019-02-04T19:58:15.511Z",
   "from":{
      "user":{
         "id":"id-value",
         "displayName":"Joh Doe",
         "userIdentityType":"aadUser"
      }
   },
   "body":{
      "contentType":"html",
      "content":"Hello World"
   }
}

参考文档:https://docs.microsoft.com/en-us/microsoftteams/platform/graph-api/import-messages/import-external-messages-to-teams