简体   繁体   English

在没有UI的情况下使用Office 365 REST API

[英]Consume Office 365 REST API Without UI

I need to push calendar entries in to a client's Outlook account. 我需要将日历条目推送到客户端的Outlook帐户。 This is fairly straight forward with Exchange. 这与Exchange非常直接。 You just authenticate with a user that has access, and then you can push entries in to other user's accounts. 您只需对具有访问权限的用户进行身份验证,然后您就可以将条目推送到其他用户的帐户。 It seems to be completely different in Office 365. 在Office 365中似乎完全不同。

I tried to follow the instructions here: https://dev.outlook.com/restapi/getstarted 我试着按照这里的说明操作: https//dev.outlook.com/restapi/getstarted

I created the app and got the app's client ID. 我创建了应用程序并获得了应用程序的客户端ID。 But, all of the documentation is around oAuth. 但是,所有文档都围绕着oAuth。 Generally speaking, oAuth is designed for scenarios when a user needs to enter their credentials in through a browser window that will then confirm with the user which credentials they are willing to allow the app to have. 一般来说,oAuth是为用户需要通过浏览器窗口输入凭据而设计的,然后用户会向用户确认他们愿意允许该应用拥有哪些凭据。

This does not match my scenario. 这与我的情况不符。 I need to be able to push the calendar entries in to the account without any UI. 我需要能够在没有任何UI的情况下将日历条目推送到帐户。 This is back end integration. 这是后端集成。 It just needs to do its job silently. 它只需要默默地完成它的工作。

I looked at this sample app: https://github.com/OfficeDev/O365-Win-Snippets 我看了这个示例应用程序: https//github.com/OfficeDev/O365-Win-Snippets

But, this is a front end app. 但是,这是一个前端应用程序。 When it needs to authenticate, it pops up a window to force the user to enter their credentials. 当需要进行身份验证时,会弹出一个窗口强制用户输入其凭据。

When I try to call the REST API that is mentioned in the getting started page, it returns HTML. 当我尝试调用入门页面中提到的REST API时,它会返回HTML。 This is the Url it mentions: 这是它提到的网址:

https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id= &redirect_uri=http%3A%2F%2Flocalhost%2Fmyapp%2F&response_type=code&scope=https%3A%2F%2Foutlook.office.com%2Fmail.read https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=&redirect_uri = http%3A%2F%2Flocalhost%2Fmyapp%2F&response_type = code&scope = https%3A%2F%2Foutlook.office.com% 2Fmail.read

I've tried a few permutations of this Url with my client ID. 我已尝试使用我的客户端ID对此Url进行了一些排列。 I've tried passing in my Office 365 credentials through basic http authentication. 我尝试通过基本的http身份验证传递我的Office 365凭据。

I'm stuck. 我被卡住了。

The answer is simple. 答案很简单。 Use the Exchange API - not Office 365 API. 使用Exchange API - 而不是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. 我很困惑,因为我认为Office 365与Exchange不同,但Office 365电子邮件服务器只是一个巨大的Exchange服务器。 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. 这是登录Office 365的Exchange服务器并将日历条目发送到电子邮件地址的示例。 Simple. 简单。

I made a wild guess about the exchange Url and it was correct: https://outlook.office365.com/ews/exchange.asmx 我粗略猜测了交换网址,这是正确的: 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); 

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. 对于初学者,任何需要Office 365集成的应用程序还必须与关联的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 有关此组件的详细摘要,请参见此处: 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 我还没有尝试过这个,但它已在此处记录并且看起来应该满足您的需求(请参阅守护程序或Web应用程序的Web应用程序部分): https//azure.microsoft.com/en-us/documentation/articles /主动目录的验证,场景/#守护程序或服务器应用到Web的API

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). 为了调用Office 365 REST API,该应用程序需要Azure Active Directory中的访问令牌,这就是您需要(强制)在Microsoft Azure Active Directory(Azure AD)中注册应用程序的原因。 Your Office 365 account in turn needs to be associated with Azure AD . 您的Office 365帐户又需要与Azure AD关联 This answer summarizes on how to register app in Azure AD in order to consume Office 365 API. 此答案总结了如何在Azure AD中注册应用程序以使用Office 365 API。

Basic authentication scheme Basic认证方案

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. 重新启动Basic身份验证,当前它已启用 API版本1.0 ,以下示例演示如何在.NET应用程序中使用Outlook Calendar REST API

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"]);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM