An API that connects multiple Microsoft services, enabling data access and automation across platforms
Precondition Failed Error while trying to send Message to Teams-Chat | Graph API | C#
Hello, 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);
}
}
}
Microsoft Security | Microsoft Graph
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
1 answer
Sort by: Most helpful
-
Deleted
This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.
2 deleted comments
Comments have been turned off. Learn more