0

Well, I would like to get a calendars data from outlook. My purpose is making small service in Python which can read & write in someones calendar in outlook account of course I suppose that I was provided access to it in Azure Active Directory. Before writing this, I read a lot of guides on how to do this. Also I tried to find similar issues on github, some things I've learned from this.

So I've made following steps. I wish you pointed on my mistakes and told how to fix them.

  • enter image description here
  • enter image description here
  • enter image description here
  • enter image description here
  • enter image description here
  • enter image description here Then I've written this code in Python with O365
from O365 import Account

CLIENT_ID = 'xxxx'
SECRET_ID = 'xxxx'
TENANT_ID = 'xxxx'

credentials = (CLIENT_ID, SECRET_ID)
account = Account(credentials, auth_flow_type='credentials', tenant_id=TENANT_ID)
if account.authenticate():
    print('Authenticated!')


schedule = account.schedule(resource='user@domain')
calendar = schedule.get_default_calendar()
events = calendar.get_events(include_recurring=False)

for event in events:
    print(event)

Then if I use email which is shown in user contact info in my directory as a resourse. I get this error:

requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://graph.microsoft.com/v1.0/users/user@domain/calendar | Error Message: The tenant for tenant guid 'xxxx' does not exist.

Then if I use 'User Principal Name', which is shown in user identity in my directory as a resourse. I get this error:

requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: https://graph.microsoft.com/v1.0/users/xxxx#EXT%[email protected]/calendar | Error Message: Insufficient privileges to complete the operation.

Then if I use 'me' as a resource I also get an error:

requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://graph.microsoft.com/v1.0/me/calendar | Error Message: /me request is only valid with delegated authentication flow.

Could you tell me what should i provide as a resource to get someones calendar or maybe what should i fix?

2
  • Please check this it may help you : github.com/O365/python-o365#calendar Commented Dec 5, 2021 at 16:43
  • @RahulKumarShaw-MT thank you for commenting. I guess I've found the solution Commented Dec 5, 2021 at 18:33

2 Answers 2

0

let's best go through the error messages:

  1. Error Message: The tenant for tenant guid 'xxxx' does not exist.

I am assuming that this is an Office 365 Business. Also that you are logged into Azure with your company address. Then you should see under: "App registrations>test feature> Overview" you should find the valid TenantID.

Tenant ID

  1. Error Message: Insufficient privileges to complete the operation.

I cant find a Scope in your example e.g:

from O365 import Account

CLIENT_ID = 'xxxx'
SECRET_ID = 'xxxx'
TENANT_ID = 'xxxx'

credentials = (CLIENT_ID, SECRET_ID)

scopes = ['https://graph.microsoft.com/Calendar.ReadWrite', 
          'https://graph.microsoft.com/User.Read', 
          'https://graph.microsoft.com/offline_access']

account = Account(credentials, tenant_id=TENANT_ID)
if account.authenticate(scopes=scopes):
   print('Authenticated!') 

you can check all Scopes here

3 . Error Message: /me request is only valid with delegated authentication flow.

I also assume a scope error here.

General recommendations:

  • Add "offline_access" as a scope - this way you don't need to log in for each run.
  • Set each scope as delegated - this limits the rights on user level - which is a reduced security risk for a company.
Sign up to request clarification or add additional context in comments.

Comments

-1

For me, joining Microsoft Developer Program and using its azure directory fixed issues.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.