Consume Office 365 REST API Without UI

前端 未结 3 679
半阙折子戏
半阙折子戏 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: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"]);
            }
        }
    }
    

提交回复
热议问题