Home > Software engineering >  Precondition Failed Error while trying to send Message to Teams-Chat | Graph API | C#
Precondition Failed Error while trying to send Message to Teams-Chat | Graph API | C#

Time:10-24

everytime I try to send a Teams-Message I recieve an error. I couldn`t figure out how to fix it and hope someone here has a clue for me.

Error Message:

Microsoft.Graph.ServiceException: "Code: PreconditionFailed Message: Requested API is not supported in application-only context Inner error: AdditionalData: date: 2022-10-20T12:07:44 request-id: 88e01bd9-370c-4739-b0bd-0244892475e2 client-request-id: 88e01bd9-370c-4739-b0bd-0244892475e2 ClientRequestId: 88e01bd9-370c-4739-b0bd-0244892475e2 "

Permissions:

Delegated Permissions: Chat.ReadBasic, Chat.Read, Chat.ReadWrite, Chat.Create, ChatMember.Read, ChatMember.ReadWrite, ChatMessage.Send, Chat.ReadWrite, Channel.Delete.All, Group.ReadWrite.All, Directory.ReadWrite.All

Application Permissions: ChatMember.Read.All, ChatMember.ReadWrite.All, Chat.ReadBasic.All, Chat.Read.All, Chat.Create, Chat.ReadWrite.All, Channel.Delete.Group, Channel.Delete.All, Group.ReadWrite.All, Directory.ReadWrite.All

Code I am using:

         /* ------------------------------------------------------------- */
     /// <summary>
     /// 
     /// </summary>
     /// <param name="userId"></param>
     /// <param name="chatID"></param>
     /// <param name="messageText"></param>
     /// <param name="scopes"></param>
     /// <returns></returns>
     public ChatMessage SendMessageToChat(string userId, string chatID, string messageText, string[] scopes = null)
     {
       return SendMessageToChatAsync(userId, chatID, messageText, scopes).GetAwaiter().GetResult();
     }
    
     /* ------------------------------------------------------------- */
    
     private async Task<ChatMessage> SendMessageToChatAsync(string userId, string chatID, string messageText, string[] scopes = null)
     {
       GraphServiceClient graphClient = this.GetAuthenticatedGraphClient(scopes);
    
       var chatMessage = new ChatMessage
       {
         Body = new ItemBody
         {
           Content = messageText
         }
       };
    
       return await graphClient.Users[userId].Chats[chatID].Messages
         .Request()
         .AddAsync(chatMessage);
    
     }
   }
 }

CodePudding user response:

what you write is await graphClient.Users[userId].Chats[chatID].Messages, so it's obvious that you want to send chat message to a teams chat but not a teams channel.

Let's see the enter image description here

  • Related