如何在没有登录提示的情况下使用 Microsoft 图形 API [英] How to use Microsoft graph API without Login prompt

查看:23
本文介绍了如何在没有登录提示的情况下使用 Microsoft 图形 API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义应用程序(桌面 C#),当我按下按钮时,它会向 Microsoft Teams 发送消息.问题是每当我第一次运行该程序时,我都需要登录我的 Microsoft 帐户.

I have a custom application (Desktop C#) when I push a button it send messages to Microsoft Teams. the problem is whenever I run the program for the first time, I need to login into my Microsoft account.

如何在没有提示的情况下发送/发布消息?(使用 Microsoft Graph API)

How to send/post message without prompt?(use Microsoft Graph API)

这是我的代码:

 private static string ClientId = "49414fb7-f415-4a84-bac8-XXXXX";
 private static string Tenant = "c7b6c891-cd42-41c4-8b44-XXXXX";
 public static IPublicClientApplication PublicClientApp;
 string[] _scopes = new string[] { "user.read","Group.ReadWrite.All" };

 private async void button1_Click(object sender, EventArgs e)
    {
        PublicClientApp = PublicClientApplicationBuilder.Create(ClientId)
       .WithAuthority(AzureCloudInstance.AzurePublic, Tenant)
       .Build();

        AuthenticationResult authResult = null;
        authResult = await PublicClientApp.AcquireTokenInteractive(_scopes).ExecuteAsync();
        var accounts = await PublicClientApp.GetAccountsAsync();
        var firstAccount = accounts.FirstOrDefault();
        authResult = await PublicClientApp.AcquireTokenSilent(_scopes, firstAccount)
             .ExecuteAsync();


        if (PublicClientApp == null)
        {
            MessageBox.Show("NULL");
            return;
        }
        if (string.IsNullOrEmpty(memoEdit1.Text.Trim()))
        {
            return;
        }


        IntegratedWindowsAuthenticationProvider authProvider = new IntegratedWindowsAuthenticationProvider(PublicClientApp, null);
        GraphServiceClient graphClient = new GraphServiceClient(authProvider);


        ChatMessage chatMessage = new ChatMessage();
        chatMessage.Subject = null;
        chatMessage.Body = new ItemBody();
        chatMessage.Body.ContentType = BodyType.Text;
        chatMessage.Body.Content = memoEdit1.Text;


        await graphClient.Teams["2ae3bf3f-e84b-4996-975c-495318cXXXXX"].Channels["19%3a63cf071a84b04f9aa381133a0e6XXXXX%40thread.skype"].Messages
            .Request()
            .AddAsync(chatMessage);
        memoEdit1.Text = "";
    }

注意:我将其创建为测试.至于生产,它需要每 1 小时自动发布一次消息,无需用户操作

Note: I create this as test. as for production it need to post message every 1 hour automatically without user action

推荐答案

为了实现我想要的,我硬编码了用户名和密码.如果有更好的答案,请不要犹豫发布它

To achieve what I want, I hardcoded the username and password. if there's a better answer please don't hesitate to post it

public partial class App
{
    static App()
    {
        _clientApp = PublicClientApplicationBuilder.Create(ClientId)
            .WithAuthority(AzureCloudInstance.AzurePublic, Tenant)
            .Build();
    }        
    private static string ClientId = "XXX-XXX";
    private static string Tenant = "XXX-XXX";

    private static IPublicClientApplication _clientApp;

    public static IPublicClientApplication PublicClientApp { get { return _clientApp; } }
}


  private async void button5_Click(object sender, EventArgs e)
    {
        string[] scopes = new string[] { "Group.ReadWrite.All" };
        UsernamePasswordProvider authProvider = new UsernamePasswordProvider(App.PublicClientApp, scopes);
        GraphServiceClient graphClient = new GraphServiceClient(authProvider);
        String password = "XXXXX";
        System.Security.SecureString sec_pass = new System.Security.SecureString();
        Array.ForEach(password.ToArray(), sec_pass.AppendChar);
        sec_pass.MakeReadOnly();
        User me = await graphClient.Me.Request()
                        .WithUsernamePassword("XXX@XXXX.com", sec_pass)
                        .GetAsync();

        //Testing post
        ChatMessage chatMessage = new ChatMessage();
        chatMessage.Subject = null;
        chatMessage.Body = new ItemBody();
        chatMessage.Body.ContentType = BodyType.Text;
        chatMessage.Body.Content = "Hello World";
        await graphClient.Teams["xxxx"].Channels["19%XXXXXX%40thread.skype"].Messages
            .Request()
            .AddAsync(chatMessage);            
    }

这篇关于如何在没有登录提示的情况下使用 Microsoft 图形 API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆