简体   繁体   English

从.net控制台应用程序调用Office 365 REST API

[英]Office 365 REST API calls from a .net console application

Please note that this question is not regarding generic REST service calling. 请注意,此问题与通用REST服务调用无关。 Its about specific Office 365 REST service API. 它涉及特定的Office 365 REST服务API。

To be specific I need to utilize the 'Contact' API here: https://msdn.microsoft.com/office/office365/APi/contacts-rest-operations#UsingtheContactsRESTAPI 具体来说,我需要在这里使用“联系”API: https//msdn.microsoft.com/office/office365/APi/contacts-rest-operations#UsingtheContactsRESTAPI

I was wondering how the Office 365 REST services can be utilized in a Console application. 我想知道如何在控制台应用程序中使用Office 365 REST服务。 There are tools to deal with the APIs from Web, mobile and windows store apps. 有一些工具可以处理来自Web,移动和Windows应用商店的API。 But I found no resource for console application. 但是我找不到控制台应用程序的资源。

I have the application created on the application registration portal here: https://apps.dev.microsoft.com 我在应用程序注册门户上创建了应用程序: https//apps.dev.microsoft.com

So I already have the Application Id, Application Secrets, Platforms Mobile application (Client Id, Redirect URI) 所以我已经有应用程序ID,应用程序秘密,平台移动应用程序(客户端ID,重定向URI)

I think I will need the authentication token (I have the Username, password). 我想我需要身份验证令牌(我有用户名,密码)。 And use that to call the REST services. 并使用它来调用REST服务。

Currently for Office 365 Mail, Calendar, and Contacts APIs two versions are supported: v1 and v2 目前,对于Office 365 Mail,Calendar和Contacts API,支持两个版本: v1v2

About REST API v2 关于REST API v2

The Office 365 API services use Azure Active Directory (Azure AD) to provide secure authentication and authorization to users' Office 365 data. Office 365 API服务使用Azure Active Directory(Azure AD)为用户的Office 365数据提供安全的身份验证和授权。 Azure AD implements authorization flows according to the OAuth 2.0 protocol . Azure AD根据OAuth 2.0协议实现授权流程。

To allow your application access to the Office 365 APIs, you need to register your application with Azure AD . 要允许应用程序访问Office 365 API,您需要在Azure AD中注册应用程序


In case of API v1 version, since it supports Basic authentication, the following example demonstrates how to read contacts in console app using user credentials: 对于API v1版本,由于它支持Basic身份验证,以下示例演示如何使用用户凭据读取控制台应用程序中的联系人:

Example

class Program
{
    static void Main(string[] args)
    {

        ReadContacts().Wait();
    }

    private static async Task ReadContacts()
    {
        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/contacts";
            var result = await client.GetStringAsync(url);

            var data = JObject.Parse(result);

            foreach (var item in data["value"])
            {
                Console.WriteLine(item["DisplayName"]);
            }
        }
    }
}

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

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