在没有 UI 的情况下使用 Office 365 REST API
Consume Office 365 REST API Without UI
我需要将日历条目推送到客户的 Outlook 帐户中。这对于 Exchange 来说相当简单。您只需通过具有访问权限的用户进行身份验证,然后就可以将条目推送到其他用户的帐户中。在 Office 365 中似乎完全不同。
我尝试按照此处的说明进行操作:
https://dev.outlook.com/restapi/getstarted
我创建了应用程序并获得了应用程序的客户端 ID。但是,所有文档都是围绕 oAuth 的。一般来说,oAuth 是为用户需要通过浏览器输入凭据的场景设计的window,然后浏览器会向用户确认他们愿意允许应用程序拥有哪些凭据。
这与我的场景不符。我需要能够在没有任何 UI 的情况下将日历条目推送到帐户中。这是后端集成。它只需要默默地完成它的工作。
我查看了这个示例应用程序:
https://github.com/OfficeDev/O365-Win-Snippets
但是,这是一个前端应用程序。当它需要进行身份验证时,它会弹出 window 以强制用户输入其凭据。
当我尝试调用入门页面中提到的 REST API 时,它 returns HTML。这是它提到的Url:
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
我用我的客户端 ID 尝试了一些 Url 的排列组合。我已尝试通过基本的 http 身份验证传递我的 Office 365 凭据。
我卡住了。
我的理解是这是可以的,但是认证看起来很复杂。对于初学者来说,任何需要 Office 365 集成的应用程序还必须与关联的 Azure AD 集成。您可以为特定用户注册您的应用程序,以便它具有您需要执行的任何操作所需的权限。有关此组件的完整摘要,请参见此处:https://msdn.microsoft.com/en-us/office/office365/howto/connect-your-app-to-o365-app-launcher?f=255&MSPPError=-2147217396#section_2
对于身份验证,您需要一个 daemon/server 应用程序模型。我还没有尝试过,但它已记录在此处,看起来应该可以满足您的需求(请参阅守护程序或服务器应用程序到 Web API 部分):https://azure.microsoft.com/en-us/documentation/articles/active-directory-authentication-scenarios/#daemon-or-server-application-to-web-api
答案很简单。使用 Exchange API - 而非 Office 365 API.
我很困惑,因为我认为 Office 365 是与 Exchange 不同的实体,但 Office 365 电子邮件服务器只是一个巨大的 Exchange 服务器。这里有一些示例代码可以很好地衡量。这是登录到 Office 365 的 Exchange 服务器并将日历条目发送到电子邮件地址的示例。简单。
我对交易所进行了大胆的猜测 Url,结果是正确的:
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);
为了调用 Office 365 REST API,该应用程序需要来自 Azure Active Directory 的访问令牌,这就是您需要(强制)在 Microsoft Azure Active Directory (Azure AD) 中注册应用程序的原因。您的 Office 365 account in turn needs to be associated with Azure AD. This answer 总结了如何在 Azure AD 中注册应用程序以使用 Office 365 API。
Basic
身份验证方案
Regrading Basic
认证,目前enabled for API version 1.0,以下示例演示如何在 .NET 应用程序中使用 Outlook Calendar REST API。
先决条件:
domain: https://outlook.office365.com/
API version: v1.0
这是一个获取我的日历并打印其名称的示例
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"]);
}
}
}
我需要将日历条目推送到客户的 Outlook 帐户中。这对于 Exchange 来说相当简单。您只需通过具有访问权限的用户进行身份验证,然后就可以将条目推送到其他用户的帐户中。在 Office 365 中似乎完全不同。
我尝试按照此处的说明进行操作: https://dev.outlook.com/restapi/getstarted
我创建了应用程序并获得了应用程序的客户端 ID。但是,所有文档都是围绕 oAuth 的。一般来说,oAuth 是为用户需要通过浏览器输入凭据的场景设计的window,然后浏览器会向用户确认他们愿意允许应用程序拥有哪些凭据。
这与我的场景不符。我需要能够在没有任何 UI 的情况下将日历条目推送到帐户中。这是后端集成。它只需要默默地完成它的工作。
我查看了这个示例应用程序: https://github.com/OfficeDev/O365-Win-Snippets
但是,这是一个前端应用程序。当它需要进行身份验证时,它会弹出 window 以强制用户输入其凭据。
当我尝试调用入门页面中提到的 REST API 时,它 returns HTML。这是它提到的Url:
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
我用我的客户端 ID 尝试了一些 Url 的排列组合。我已尝试通过基本的 http 身份验证传递我的 Office 365 凭据。
我卡住了。
我的理解是这是可以的,但是认证看起来很复杂。对于初学者来说,任何需要 Office 365 集成的应用程序还必须与关联的 Azure AD 集成。您可以为特定用户注册您的应用程序,以便它具有您需要执行的任何操作所需的权限。有关此组件的完整摘要,请参见此处:https://msdn.microsoft.com/en-us/office/office365/howto/connect-your-app-to-o365-app-launcher?f=255&MSPPError=-2147217396#section_2
对于身份验证,您需要一个 daemon/server 应用程序模型。我还没有尝试过,但它已记录在此处,看起来应该可以满足您的需求(请参阅守护程序或服务器应用程序到 Web API 部分):https://azure.microsoft.com/en-us/documentation/articles/active-directory-authentication-scenarios/#daemon-or-server-application-to-web-api
答案很简单。使用 Exchange API - 而非 Office 365 API.
我很困惑,因为我认为 Office 365 是与 Exchange 不同的实体,但 Office 365 电子邮件服务器只是一个巨大的 Exchange 服务器。这里有一些示例代码可以很好地衡量。这是登录到 Office 365 的 Exchange 服务器并将日历条目发送到电子邮件地址的示例。简单。
我对交易所进行了大胆的猜测 Url,结果是正确的: 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);
为了调用 Office 365 REST API,该应用程序需要来自 Azure Active Directory 的访问令牌,这就是您需要(强制)在 Microsoft Azure Active Directory (Azure AD) 中注册应用程序的原因。您的 Office 365 account in turn needs to be associated with Azure AD. This answer 总结了如何在 Azure AD 中注册应用程序以使用 Office 365 API。
Basic
身份验证方案
Regrading Basic
认证,目前enabled for API version 1.0,以下示例演示如何在 .NET 应用程序中使用 Outlook Calendar REST API。
先决条件:
domain: https://outlook.office365.com/
API version: v1.0
这是一个获取我的日历并打印其名称的示例
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"]);
}
}
}