Sending a chat message to a microsoft Teams channel using Microsoft Graph API C#

99封情书 提交于 2020-05-14 07:30:21

问题


My goal is simple. I want to send an automated chat message in to a MS Teams channel using the graph API.

This seems to be beta feature of the graph API and is only avalible in the Microsoft.Graph.Beta.

I have read the docs and have been trying to follow this example:

https://docs.microsoft.com/en-us/graph/api/channel-post-messages, I have all the permissions set correct in my azure portal. I keep getting 'Unknown Error' I have tried:

var graphServiceClient = MicrosoftGraphService.GetGraphServiceClient();

            var chatMessage = new ChatMessage
            {
                Subject = null,

                Body = new ItemBody
                {
                    ContentType = BodyType.Text,
                    Content = messageText
                }

            };

            var response = await graphServiceClient.Teams["77f9c17f-54ca-4275-82d4-fff7esdacda1"].Channels["2007765c-8185-4cc7-8064-fb1b10f27e6b"].Messages.Request()
               .AddAsync(chatMessage);

I have also tried to to see if I can get anything from teams:

var teams = await graphServiceClient.Teams["77f9c17f-54ca-4275-2sed4-ffsde59acda1"].Request().GetAsync();

Again all I get is Unknown error, I have used GRAPH API before to do things like get users in an organisation, so I know the genreal set up is correct.

Has anyone on the Internet somewhere in the world got this to work?! becuase its driving me crazy


回答1:


Same problem here :

Everything is ok with users or groups, but I can't get anything from Teams (unknownError)

All IDs are correct and checked

Here are the authorizations I have set for the app :

  • Read all users' teamwork activity feed
  • Read all groups
  • Send a teamwork activity to any user
  • Get a list of all teams

Here is my code (based on microsoft daemon app scenario)

The access token is ok

var graphClient = new GraphServiceClient(
    "https://graph.microsoft.com/beta",
    new DelegateAuthenticationProvider(async (requestMessage) =>
    {
        requestMessage.Headers.Authorization =
            new AuthenticationHeaderValue("Bearer", result.AccessToken);
    }));

var chatMessage = new ChatMessage
{
    Subject = "Message de test",
    Body = new ItemBody
    {
        ContentType = BodyType.Html,
        Content = "Contenu de test"
    }
};

await graphClient.Teams["218a4b1d-84d5-48a2-97a0-023e4e4c3e85"].Channels["19:adbf8ddf37a049aa9f63a0f8ee0e8054@thread.tacv2"].Messages
    .Request()
    .AddAsync(chatMessage);

And the result :

Token acquired
Code: UnknownError
Inner error:
    AdditionalData:
    request-id: e2e433d8-cedd-4401-b5b2-6f34cf5611cf
    date: 2020-03-30T12:14:15
ClientRequestId: e2e433d8-cedd-4401-b5b2-6f34cf5611cf

Edit(2020-04-01) :

No solution at the time being : there are answers to comments at the bottom of the page "Create chatMessage in a channel" in ms doc (feedback section)

It seems that applications cannot be granted the permission to send chatMessages up to now.

RamjotSingh commented on Jun 11, 2019 Contributor

@pythonpsycho1337 - As the permission table above notes, Application only context is not supported on this API at the moment.


RamjotSingh commented on Dec 16, 2019 Contributor

Supporting application permissions is something we plan to do but we do not have a date yet.


RamjotSingh commented a day ago Contributor

We will share on Microsoft Graph Blog once we have application permissions for this API. Since the original question for this issue was answered. Closing it.



来源:https://stackoverflow.com/questions/60561348/sending-a-chat-message-to-a-microsoft-teams-channel-using-microsoft-graph-api-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!