我需要使用 Graph API 將聊天訊息發送到MS Teams頻道,在此處記錄。
我從擁有所需權限的應用注冊中檢索令牌:
- ChannelMessage.Send(委托)
- ChannelMessage.Send(應用程式)
權限有Admin Consent。
這是獲取令牌的代碼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:9*********************************@thread.tacv2/messages
Authorization標頭使用C# 代碼獲取的令牌設定,有效負載如下:
{
"createdDateTime": "2022-03-07T17:47:11.7429830Z",
"from": {
"user": {
"id": "2a7*********************************",
"displayName": "User display name",
"userIdentityType": "aadUser"
}
},
"body": {
"contentType": "html",
"content": "Hello Teams!"
}
}
This REST API call perfectly works with Graph Explorer when logged as a tenant user (using only the body property). With the App Registration token and additional properties I get the following response with HTTP status code 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"
}
}
}
I don't understand how to satisfy Thread is not marked for import.
I'll appreciate any help on how to succeed in this call. If not possible: any other way to send a message using the Graph API with App Registration token.
uj5u.com熱心網友回復:
應用程式權限僅支持遷移。
請按照以下步驟操作。
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.在遷移狀態下創建New Channel
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/441858.html
標籤:c# rest azure-active-directory microsoft-graph-api microsoft-teams
上一篇:在cmd上使用newman執行時,使用郵遞員收集檔案匯出AUth2.0不記名令牌不起作用
下一篇:用python找到3維向量的大小
