Office 365 REST API calls from a .net console application

15,424

Currently for Office 365 Mail, Calendar, and Contacts APIs two versions are supported: v1 and v2

About 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. Azure AD implements authorization flows according to the OAuth 2.0 protocol.

To allow your application access to the Office 365 APIs, you need to register your application with 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:

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


  
Share:
15,424
Rahatur
Author by

Rahatur

Over the last 13+ years, I have been developing wide range of web applications and eCommerce sites using Asp.net, MVC, Web API, C#, Telerik, SQL Server, HMTL5, JavaScript, Jquery, Bootstrap and Ajax. I have been working for startup companies and small/medium businesses. My core competency lies in complete end-end management of web applications, web services and back-end services for mobile applications. I am seeking opportunities to build web and mobile applications from the ground up for you or your business. I also have experiences in the following areas: Software Architecture, Analysis, Website Security, Web/REST services, mobile application development, OOP, software design and testing. I have been developing desktop, web and mobile applications based on: C#, Asp.net MVC, Web API, Web security, Entity Framework, SignalR etc. eCommerce, Payment gateway, Shopping Cart, nopCommerce framework CSS, xHtml, Html, Psd to Html, DHtml Webservice, Ajax, Json, SOAP, XML Database, MSSQL, MySQL, MS Access Pdf, Excel, CSV JavaScript, Jquery, AngularJs Experience with 3rd party API integration Web Scraper, Data Mining, Business Intelligence, Reporting I consider myself self-taught and always willing to acquire additional necessary skills. I only apply for the jobs which correspond to my skills. I really enjoy working on the chosen projects. Which allows me to continue with a higher rate of client satisfactions and thus excellent ratings. I am available on Skype and email for most part of the day. Do you need to discuss a project? Please feel free to knock me on Skype. I will be glad to assist you on after delivery if you have any question.

Updated on June 08, 2022

Comments

  • Rahatur
    Rahatur almost 2 years

    Please note that this question is not regarding generic REST service calling. Its about specific Office 365 REST service API.

    To be specific I need to utilize the 'Contact' API here: 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. There are tools to deal with the APIs from Web, mobile and windows store apps. But I found no resource for console application.

    I have the application created on the application registration portal here: https://apps.dev.microsoft.com

    So I already have the Application Id, Application Secrets, Platforms Mobile application (Client Id, Redirect URI)

    I think I will need the authentication token (I have the Username, password). And use that to call the REST services.