1

I need to get the events for the current day from a personal Outlook calendar. I have found next to no feasible resources online besides maybe Microsoft's tutorial (https://learn.microsoft.com/en-us/graph/tutorials/python), but I do not want to build a Django app. Can anyone provide some other resources?

also: I have seen a lot of ppl calling APIs by using a GET <url> command. I cannot for the life of me understand how or where you can use this? Am I missing something crucial when it comes to using APIs?

Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150
Dragos Spiridon
  • 51
  • 1
  • 2
  • 10
  • 1
    It's worth you skimming the tutorial anyway. These two lines `# Send GET to /me/events` and `events = requests.get` explain both your questions. I would suggest you learn what a REST API is and google `outlook rest api`. – Alan Nov 27 '21 at 16:49

2 Answers2

1

First you should know that if you wanna call ms graph api, you need to get the access token first and add it to the request header like screenshot below. What I showed in the screenshot is create calendar events but they're similar. Therefore, you can't avoid to generate the token.

Then there're 2 ways lie in front of you, if you are composing a web app, then you can follow this section to find a suitable sample for you, and if you are composing a daemon application, that means you need to use clientcredentialflow here and you may refer to this section.

Anyway, whatever you use SDK or sending http request to call the api, you all need to choose a suitable flow to obtain access token.

enter image description here

Tiny Wang
  • 10,423
  • 1
  • 11
  • 29
1

For this purpose without using Microsoft Graph API via request in python, there is a PyPI package named O365.

By the following procedure you can easily read a Microsoft calendar:

  1. install the package: pip install O365
  2. register an application in the Microsoft Azure console and keep the application (client) id as well as client secret — this article can help you up.
  3. check the signInAudience, it should be AzureADandPersonalMicrosoftAccount not PersonalMicrosoftAccount within Microsft Azure Manifest, otherwise, you can edit that.
  4. next you should set delegated permission to what scopes you want, in your case it's Calendars.Read. Here's a snapshot of my configuration in Azure:

enter image description here

Now it's time to dive into the code:

from O365 import Account

CLIENT_ID = "xxx"
CLIENT_SECRET = "xxx"

credentials = (CLIENT_ID, CLIENT_SECRET)
scopes = ['Calendars.Read']
account = Account(credentials)

if not account.is_authenticated:
    account.authenticate(scopes=scopes)
    print('Authenticated!')

schedule = account.schedule()
calendar = schedule.get_default_calendar()
events = calendar.get_events(include_recurring=False) 

for event in events:
    print(event)
Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150