creating new events in calendar with O365 api python

zerkerpure 6 Reputation points
2022-04-10T04:45:52.447+00:00

Hello 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. Here is my code below any help would be greatly appreciated.
It's stating this error:

Unable to fetch auth token. Error: (invalid_scope) AADSTS1002012: The provided value for scope basic https://graph.microsoft.com/.default/Calendars.ReadWrite.Shared is not valid. Client credential flows must have a scope value with /.default suffixed to the resource identifier (application ID URI).

Client Error: 401 Client Error: Unauthorized for url: https://graph.microsoft.com/v1.0/calendar | Error Message: Access token is empty.

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 = ['basic','https://graph.microsoft.com/.default/Calendars.ReadWrite.Shared']
account = Account(credentials,auth_flow_type= 'credentials', tenant_id= My_Tenant_Id)

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

schedule = account.schedule()
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()

Microsoft Security Microsoft Graph
{count} votes

1 answer

Sort by: Most helpful
  1. CarlZhao-MSFT 46,371 Reputation points
    2022-04-11T06:21:07.927+00:00

    Hi @zerkerpure

    The client credential flow does not support dynamic permissions, so you should remove /Calendars.ReadWrite.Shared. The /.default scope already contains all application permissions.

    The correct script should be:

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

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.