Consume Office 365 REST API Without UI

前端 未结 3 656
半阙折子戏
半阙折子戏 2021-01-05 16:22

I need to push calendar entries in to a client\'s Outlook account. This is fairly straight forward with Exchange. You just authenticate with a user that has access, and then

相关标签:
3条回答
  • 2021-01-05 16:37

    The answer is simple. Use the Exchange API - not Office 365 API.

    I was confused because I assumed that Office 365 was a different entity to Exchange, but the Office 365 email server just is one giant Exchange server. Here's some sample code for good measure. This is an example of logging in to Office 365's Exchange server and sending off a calendar entry to an email address. Simple.

    I made a wild guess about the exchange Url and it was correct: https://outlook.office365.com/ews/exchange.asmx

        //Connect to exchange
        var ewsProxy = new ExchangeService(ExchangeVersion.Exchange2013);
        ewsProxy.Url = new Uri("https://outlook.office365.com/ews/exchange.asmx");
    
        //Create the meeting
        var meeting = new Appointment(ewsProxy);
    
        ewsProxy.Credentials = new NetworkCredential(_Username, _Password);
        meeting.RequiredAttendees.Add(_Recipient);
    
        // Set the properties on the meeting object to create the meeting.
        meeting.Subject = "Meeting";
        meeting.Body = "Please go to the meeting.";
        meeting.Start = DateTime.Now.AddHours(1);
        meeting.End = DateTime.Now.AddHours(2);
        meeting.Location = "Location";
        meeting.ReminderMinutesBeforeStart = 60;
    
        // Save the meeting to the Calendar folder and send the meeting request.
        meeting.Save(SendInvitationsMode.SendToAllAndSaveCopy); 
    
    0 讨论(0)
  • 2021-01-05 16:41

    In order to call the Office 365 REST API, the app requires an access token from Azure Active Directory, that's why you need (mandatory) to register app in Microsoft Azure Active Directory (Azure AD). Your Office 365 account in turn needs to be associated with Azure AD. This answer summarizes on how to register app in Azure AD in order to consume Office 365 API.

    Basic authentication scheme

    Regrading Basic authentication, currently it is enabled for API version 1.0, the following example demonstrates how to consume Outlook Calendar REST API in .NET application.

    Prerequisites:

    domain: https://outlook.office365.com/
    API version: v1.0
    

    Here is an example that gets my calendars and prints its names

    private static async Task ReadCalendars()
    {
        var handler = new HttpClientHandler();
        handler.Credentials = new NetworkCredential()
        {
            UserName = ConfigurationManager.AppSettings["UserName"],
            Password = ConfigurationManager.AppSettings["Password"]
        };
    
        using (var client = new HttpClient(handler))
        {
            var url = "https://outlook.office365.com/api/v1.0/me/calendars";
            var result = await client.GetStringAsync(url);
    
            var data = JObject.Parse(result);
            foreach (var item in data["value"])
            {
                Console.WriteLine(item["Name"]);
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-05 17:00

    My understanding is that this is possible, but the authentication looks quite complicated. For starters, any application that requires Office 365 integration must also integrate with the associated Azure AD. You can register your application for specific users so that it has the permissions required for whatever operations you need to perform. See here for a good summary of this component: https://msdn.microsoft.com/en-us/office/office365/howto/connect-your-app-to-o365-app-launcher?f=255&MSPPError=-2147217396#section_2

    For authentication, you require a daemon/server application model. I've not attempted this yet, but it's documented here and looks like it should meet your needs (see the Daemon or Server Application to Web API section): https://azure.microsoft.com/en-us/documentation/articles/active-directory-authentication-scenarios/#daemon-or-server-application-to-web-api

    0 讨论(0)
提交回复
热议问题