Get Started with MS Graph API

I’m sure that if you were looking to get comprehensive reporting from your Microsoft Azure and Office 365 subscription, you would have come across Microsoft Graph Restful APIs. What does it take to get started with these APIs, and what do you have to learn to use them properly? Do you have to be a developer?

Several weeks ago, I started a new project where I wanted to work out the sizing of the storage space needed to back up the Office 365 Tenant, including the OneDrive. I was looking for an easy way to calculate my design requirements, and then I came across the Microsoft Graph API.  I was impressed by the API’s diversity of reporting and decided to learn more about it. I am going to share my experience with you, and in the process, provide two simple examples to start you off. So let’s begin:

Azure App Registrations

To get started with today’s blog exercise, we must first create and register an App on the Azure subscription. To do that, we must follow these steps:

  • Login to your Azure subscription; browse to the Azure Active Directory.
  • Choose App Registrations from the menu.
  • Click on New Application Registration at the top left side of the side working area; create a new App:

Screen Shot 2019-01-16 at 8.06.51 am

After the App been created,  save the Application ID; we are going to need it later:

Screen Shot 2019-01-16 at 8.09.07 am.png

After the Application ID has been copied, select Settings from the menu bar at the top. Select Required Permissions; then add Microsoft Graph and Office 365 Management API.”

Screen-Shot-2019-01-16-at-8.23.51-am.png

For Permissions, you can check the required permissions using the following link: https://docs.microsoft.com/en-us/graph/auth-v2-service?view=graph-rest-1.0

On this blog post, and as it is a test environment, I will grant all the permissions available.

To authenticate with the Application ID, we must create a new Key. We can do this from the Key option at the Setting menu:

Note: You must copy the key Password before leaving the screen, and after pressing the Save button at the top. If you don’t do this, you will not be able to get the password back.

The last thing we have to do before we get to the fun part is to retrieve some code from the Microsoft Graph API webpage to get our Azure tenant authentication and Token URLs. To get these URLs, press the Endpoints from the top menu at Application Registrations:

Screen Shot 2019-01-16 at 8.30.18 am

Now we are ready to start experiencing the Graph API coding

Retrieve Token

To retrieve the Token to get started with the Graph API, we must put all our previous steps together. To simplify the task, I will use the Insomnia REST Client. You can download it from this link. An option to the Insomnia REST Client is PostMan, it will give you the same result.

Open the Insomnia REST Client, or Postman, and then create a new Request. On successful authentication, TAB change the authentication method to OAuth2. Fill in the parameters where indicated.

Screen Shot 2019-01-16 at 9.37.21 am

Note 1: All the information required to complete the fields in the screenshot above is that info we gathered in the previous steps.

Note 2: You must add “?resource=https%3A%2F%2Fmanage.office.com” to the end of the Authorization URL.

Next, type the following information on the Body TAB as From URL Encoded:

Screen Shot 2019-01-16 at 10.46.33 am.png

Running the that request above will return the Token we have to use from now on. Of course, to successfully run the request, you must provide your Azure subscription username and password.

Screen Shot 2019-01-16 at 9.50.01 am.png

Note: To learn more about the Token retrieval process, browse to this URL: https://docs.microsoft.com/en-us/graph/auth-v2-service?view=graph-rest-1.0

Example 1 GetOffice365ActiveUserDetail :

Now, let’s put everything together and begin using the Microsoft Graph APIs. Start by running a query on the Microsoft Graph API to return a list of all the users on our Office 365. To do this, we need these items:

  • The requested URL
  • The token

The requested URL we can find on this link,; the token we can copy from the previous step. All that remains of this task is to put it all together:

  • Create a new Insomnia REST Client or Postman request.
  • At the request URL, use the URL from the MS link provided; that is: GET https://graph.microsoft.com/v1.0/users
  • On the header, pass the token as Authorization: Bearer
  • After the request has executed, you will have a list containing the details of all your users on your Office365 subscription. You see a sample below:

Screen-Shot-2019-01-16-at-10.20.52-am.png

Example 2 GetOneDriveUsageStorage:

This example is very similar to the previous example; we need the URL and the token to query the OneDrive Storage usage for a 7 day period. The URL to the query can be found on this link. Follow these steps to run it:

  • Create a new Insomnia REST Client or Postman request.
  • At the request URL, use the URL from the MS link provided at GET https://graph.microsoft.com/v1.0/reports/getOneDriveUsageStorage(period=’D7′)

Note: D7 = 7 Days

  • On the header, we must pass the token as Authorization: Bearer
  • After the request has executed, you will have a report on your OneDrive Storage usage for the 7 day period:

Screen Shot 2019-01-16 at 10.40.42 am

Summary

On this blog post I simplified for you the experience of learning the Microsoft Graph API. The steps I described above will get you started, and with the help of the Insomnia REST Client and Postman interfaces, you can quickly start using the Microsoft Graph API without writing a single piece of code. As a Solutions Architect, I use those queries to better understand how customers are using their Office 365 to get the size they need for their Office 365 backups. I hope this blog helps you to learn a new skill and gets you started with the Microsoft Graph API. One final thing, the links I provided in this blog post are from the official Microsoft articles; if you want more information for a better understanding of the APIs, they are easily accessible from your browser.

Leave a Reply