Microsoft.Exchange.WebServices.Data;

using Microsoft.Exchange.WebServices.Data;

using Microsoft.Identity.Client;

using System;

using System.Configuration;

 

namespace EwsOAuth

{

    class Program

    {

        static async System.Threading.Tasks.Task Main(string[] args)

        {

            // Using Microsoft.Identity.Client 4.22.0

            var cca = ConfidentialClientApplicationBuilder

                .Create(ConfigurationManager.AppSettings["appId"])

                .WithClientSecret(ConfigurationManager.AppSettings["clientSecret"])

                .WithTenantId(ConfigurationManager.AppSettings["tenantId"])

                // Use the next line of code only in a 21V environment

                .WithAuthority(AzureCloudInstance.AzureChina, ConfigurationManager.AppSettings["tenantId"])

                .Build();

            //the permission scope required for EWS access

            var ewsScopes = new string[] { "https://partner.outlook.cn/.default" };

 

            try

            {

                //make the token request

                var authResult = await cca.AcquireTokenForClient(ewsScopes).ExecuteAsync();

 

                // Configure the ExchangeService with the access token

                var ewsClient = new ExchangeService();

                ewsClient.Url = new Uri("https://partner.outlook.cn/EWS/Exchange.asmx");

                ewsClient.Credentials = new OAuthCredentials(authResult.AccessToken);

                ewsClient.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "S02@ms10.online");

 

                //Include x-anchormailbox header

                ewsClient.HttpHeaders.Add("X-AnchorMailbox", "S02@ms10.online");

 

                // Make an EWS call

                               

                DateTime endDate = DateTime.Now;

                DateTime startDate = endDate.AddDays(-30);

                CalendarFolder calendar = CalendarFolder.Bind(ewsClient, WellKnownFolderName.Calendar, new PropertySet());

                CalendarView cView = new CalendarView(startDate, endDate);

                cView.PropertySet = new PropertySet(AppointmentSchema.Subject, AppointmentSchema.Start, AppointmentSchema.End);

                FindItemsResults<Appointment> appointments = calendar.FindAppointments(cView);

                Console.WriteLine("\nThe first " + " appointments on your calendar from " + startDate.Date.ToShortDateString() +

             " to " + endDate.Date.ToShortDateString() + " are: \n");

                foreach (Appointment a in appointments)

                {

                    Console.Write("Subject: " + a.Subject.ToString() + " ");

                    Console.Write("Start: " + a.Start.ToString() + " ");

                    Console.Write("End: " + a.End.ToString());

                    Console.WriteLine();

                }

            }

            catch (MsalException ex)

            {

                Console.WriteLine($"Error acquiring access token: {ex}");

            }

            catch (Exception ex)

            {

                Console.WriteLine($"Error: {ex}");

            }

 

            if (System.Diagnostics.Debugger.IsAttached)

            {

                Console.WriteLine("Hit any key to exit...");

                Console.ReadKey();

            }

        }

    }

}

 

posted @ 2023-05-15 14:29  启明星工作室  阅读(89)  评论(0编辑  收藏  举报