How to access calendar events of shared calendars via o365 and Microsoft Graph API?

760 Views Asked by At

I have successfully been able to access my own calendar events by registering an app in Azure and calling the Microsoft Graph API via o365. Here is the code I've been using, thanks to an article I found online:

from O365 import Account, MSGraphProtocol

#gets us access to the API
CLIENT_ID = 'my client id'
SECRET_ID = 'my secret id'
credentials = (CLIENT_ID, SECRET_ID)
protocol = MSGraphProtocol()
scopes = ['https://graph.microsoft.com/.default']
account = Account(credentials, protocol=protocol)
if account.authenticate(scopes=scopes):
   print('Authenticated!')

#calls calendar event
schedule = account.schedule()
calendar = schedule.get_default_calendar()

From here, I am able to access the events from my own calendar.

But ultimately, I need to be able to access the events from a shared calendar.

Based on the documentation I found for o365, I tried changing the "resource" to the email of the shared calendar I would like to access. My code looks like this:

schedule2 = account.schedule(resource = '[email protected]')
calendar2 = schedule2.get_default_calendar()

Then, I get this error once I call the get_default_calendar() (i.e. defining calendar2) function:

Client Error: 404 Client Error: Not Found for url: https://graph.microsoft.com/v1.0/users/[email protected]/calendar | Error Message: The requested user '[email protected]' is invalid.

...

    raise HTTPError('{} | Error Message: {}'.format(e.args[0], error_message), response=response) from None
requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://graph.microsoft.com/v1.0/users/[email protected]/calendar | Error Message: The requested user '[email protected]' is invalid.

Now, the email definitely exists and has its own outlook account. Also, my API permissions include Calendars.Read.Shared. I'm guessing the issue is that this is not the proper way to get the shared calendar, but relentless googling hasn't shown me another way. I've also looked at this article and I don't understand how to use this information in the context of o365.

Is the issue within the resource I plugged in? Or is there an alternative way to access a shared calendar? I am a beginner python user so any insight is appreciated. I feel like this is a rather simple task but I am just missing the correct function. Thanks!

1

There are 1 best solutions below

0
On

I tried in my environment and got same error below results:

enter image description here

404 Client Error: Not Found for url:
https://graph.microsoft.com/v1.0/users/[email protected]/calendar | Error Message: The requested user '[email protected]' is invalid. 

The above error message indicates that the user "[email protected]" does not exist in the Microsoft Graph API.

This could be due to a few reasons, such as the user account being deleted, the account being inactive or suspended, or an incorrect username or email address being provided.

Check the license for the account and you can use the same code to access the events from a shared calendar:

Code:

from O365 import Account, MSGraphProtocol

#gets us access to the API
CLIENT_ID = 'xxx'
SECRET_ID = 'xxx'
credentials = (CLIENT_ID, SECRET_ID)
protocol = MSGraphProtocol()
scopes = ['https://graph.microsoft.com/.default']
account = Account(credentials, protocol=protocol)
if account.authenticate(scopes=scopes):
        print("Authenticated")
        schedule1 = account.schedule(resource = '[email protected]')
        calendar1= schedule1.get_default_calendar()
        events = calendar1.get_events(include_recurring=False) 
      

Also you can verify with graph explorer by using this Document.

Query:

https://graph.microsoft.com/v1.0/users/{userid or userprinciplename}/calendars

Output:

enter image description here

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('xxxxxxxx')/calendars",
    "value": [
        {
            "id": "AAMkADM1NzUwMTUxLWJlZjQtNDFkYi1iNGRjLWE0ODllYTlkN2YxNABGAAAAAACBV9bxxxxxxxx",
            "name": "Calendar",
            "color": "auto",
            "hexColor": "",
            "isDefaultCalendar": true,
            "changeKey": "s8hj4slJ2Uym2iExxxxxx",
            "canShare": true,
            "canViewPrivateItems": true,
            "canEdit": true,
            "allowedOnlineMeetingProviders": [],
            "defaultOnlineMeetingProvider": "unknown",
            "isTallyingResponses": true,
            "isRemovable": false,
            "owner": {
                "name": "username",
                "address": "[email protected]"
            }
        },
        {
            "id": "AAMkADM1NzUwMTUxLWJlZjQtNDFkYi1iNGRjLWE0ODllYTlkN2YxNABGAAAAAACBV9bfxxxxxxxxxxx",
            "name": "United States holidays",
            "color": "auto",
            "hexColor": "",
            "isDefaultCalendar": false,
            "changeKey": "s8hj4slJ2Uym2ixxxxxxxx",
            "canShare": false,
            "canViewPrivateItems": true,
            "canEdit": false,
            "allowedOnlineMeetingProviders": [],
            "defaultOnlineMeetingProvider": "unknown",
            "isTallyingResponses": false,
            "isRemovable": true,
            "owner": {
                "name": "username",
                "address": "[email protected]"
            }
        }
     ]
  }