Graph API:向通道发送消息失败,因为“禁止”(线程未标记为导入)

问题描述 投票:0回答:2

我需要使用 Graph API 将聊天消息发送到 MS Teams 频道,记录在此处

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

  • ChannelMessage.Send(委托)
  • ChannelMessage.Send(应用程序)

权限已获得管理员同意

这是获取令牌的代码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

使用C#代码获取的令牌设置Authorization标头,有效负载如下:

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

以租户用户身份登录时(仅使用

body
属性),此 REST API 调用可以与 Graph Explorer 完美配合。通过应用程序注册令牌和其他属性,我得到以下带有 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

如果您能提供有关如何成功完成本次通话的任何帮助,我将不胜感激。如果不可能:使用具有应用程序注册令牌的 Graph API 发送消息的任何其他方式。

c# rest azure-active-directory microsoft-graph-api microsoft-teams
2个回答
2
投票

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

请按照以下步骤操作。

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

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

Content-Type: application/json
{
  "@microsoft.graph.teamCreationMode": "migration",
  "[email protected]": "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://learn.microsoft.com/en-us/microsoftteams/platform/graph-api/import-messages/import-external-messages-to-teams


0
投票

尝试导入消息时,我收到错误: “发送了无效的请求正文”

我正在传递这个: { "createdDateTime":"2019-02-04T19:58:15.511Z", “从”:{ “用户”:{ "id":"id 值", "displayName":"约翰·多伊", “用户身份类型”:“aadUser” } }, “身体”:{ “内容类型”:“html”, “内容”:“你好世界” } } 请问这里有人可以帮忙吗?

© www.soinside.com 2019 - 2024. All rights reserved.