Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
437 views
in Technique[技术] by (71.8m points)

c# - Adding a Teams Channel Using MS Graph

I am trying to create a channel using MS Graph within a BotFramework bot. I get what appears to be a valid access Token. However the code below generates the following error:

The collection type 'Microsoft.Graph.IChannelMembersCollectionPage' on 'Microsoft.Graph.Channel.Members' is not supported.

var credential = new DefaultAzureCredential();
var token = credential.GetToken(
    new Azure.Core.TokenRequestContext(
        new[] { "https://graph.microsoft.com/.default" }));

var accessToken = token.Token;
Logger.LogWarning($"Token:{accessToken.ToString()}");
var graphServiceClient = new GraphServiceClient(
    new DelegateAuthenticationProvider((requestMessage) =>
    {
        requestMessage
        .Headers
        .Authorization = new AuthenticationHeaderValue("bearer", accessToken);

        return Task.CompletedTask;
    }));


try
{

    var chan = new Channel
    {
        DisplayName = $"Chan1",
        Description = "This channel is where we debate all future world domination plans",
        MembershipType = ChannelMembershipType.Standard
    };


    await graphServiceClient.Teams["{GroupID}"].Channels.Request().AddAsync(chan);
}
question from:https://stackoverflow.com/questions/65854507/adding-a-teams-channel-using-ms-graph

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use Graph SDK to generate token internally. Please try providing application permissions in azure portal and use the below code to create a channel in MS Teams. Below are the packages you need to install.

This is an example for application permissions. You can try the same code with minor changes/to no changes for delegate permissions.

enter image description here

           string clientId = "";
            string clientSecret = "1";
            string tenantId = "";
          
       

            IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
    .Create(clientId)
    .WithTenantId(tenantId)
    .WithClientSecret(clientSecret) // or .WithCertificate(certificate)
    .Build();

            //AuthorizationCodeProvider authProvider = new AuthorizationCodeProvider(confidentialClientApplication, scopes);

            ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);


            GraphServiceClient graphClient = new GraphServiceClient(authProvider);

            var channel = new Channel
            {
                DisplayName = "Topic Discussion",
                Description = "This channel is where we debate all future architecture plans",
                MembershipType = ChannelMembershipType.Standard
            };

            await graphClient.Teams["{Your-teams-id}"].Channels
                .Request()
                .AddAsync(channel);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...