Why can not I use the O365 package in python?

Viewed 143

Here is the code that I am using to interactive with Office 365:

from O365 import Account
credentials = ('...', '...')
account = Account(credentials)
if account.authenticate(scopes=['basic', 'message_all']):
   print('Authenticated!')

After I run the python file, I get a URL, and I click the URL:

AADSTS50194: Application '...'(test) is not configured as a multi-tenant application. Usage of the /common endpoint is not supported for such applications created after '10/15/2018'. Use a tenant-specific endpoint or configure the application to be multi-tenant.

I set "Accounts in this organizational directory only" because I only want the people in my company can access the app.

I also set this (as you can see, I have set the redirect URL ): enter image description here

Am I missing something here?

Here is my new code:

from O365 import Account

credentials = ('..', '..')

# the default protocol will be Microsoft Graph

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

storage = account.storage()
print(storage)
my_drive = storage.get_default_drive()  # or get_drive('drive-id')
print(my_drive)
root_folder = my_drive.get_root_folder()
attachments_folder = my_drive.get_special_folder('attachments')

It can get "Authenticated!", but I get the following error:

Client Error: 403 Client Error: Forbidden for url: https://graph.microsoft.com/v1.0/drive/root | Error Message: Either scp or roles claim need to be present in the token.

{"error":{"code":"InvalidAuthenticationToken","message":"Access token is empty.","innerError":{"date":"2022-04-07T01:24:20","request-id":"be71cdcf-6f69-4d6f-b344-588d0b693253","client-request-id":"be71cdcf-6f69-4d6f-b344-588d0b693253"}}}

Thanks

1 Answers

You need to find your tenant Id (it will be listed on the application registration)

enter image description here

and then pass that in eg

from O365 import Account

credentials = ('my_client_id', 'my_client_secret')

# the default protocol will be Microsoft Graph

account = Account(credentials, auth_flow_type='credentials', tenant_id='my-tenant-id')
if account.authenticate():
   print('Authenticated!')

Related