'Creating a Calendar event O365 package in python?

I'm trying to make a script to add Calendar events to my main calendar in outlook using python and the O365 package. But I keep running into various issues with creating a sample event. any help would be greatly appreciated.

EDIT UPDATED CODE WITH NEW PROBLEM: Still getting a 401 error but now its saying my token is unauthorized/no permissions.

The error:

Authenticated!
Client Error: 401 Client Error: Unauthorized 
for url: https://graph.microsoft.com/v1.0/users/[email protected]/calendar
Error Message: The token contains no permissions, or permissions can not be understood

code is below:

from O365 import Account, MSGraphProtocol
import win32com.client as client
from O365 import Protocol
import datetime as dt

protocol_graph = MSGraphProtocol()
scopes_graph = ['https://graph.microsoft.com/.default']
account = Account(credentials,auth_flow_type= 'credentials', tenant_id= My_Tenant_Id)

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

schedule = account.schedule(resource= '[email protected]')
calendar = schedule.get_default_calendar()
new_event = calendar.new_event()
new_event.subject = 'Recruit George!'
new_event.location = 'Courthouse'
new_event.start = dt.datetime(2022,4,20)
new_event.save()

api permissions: enter image description here

UPDATE here is the auth token results from Jwt.ms image enter image description here



Solution 1:[1]

As suggested by @Shiva Keshav Varma & CarlZhao-MSFT,


The error you are getting, Because dynamic permissions are not supported by the client credential flow, you should remove /Calendars. ReadWrite. Shared. All application rights are already in the /.default scope.


To use the correct script you can try with following;

 scopes_graph = ['basic','https://graph.microsoft.com/.default']

please refer this link for similar discussion : MS Q&A

For more information please refer the below links:

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 AjayKumarGhose-MT