python-o365 icon indicating copy to clipboard operation
python-o365 copied to clipboard

How to read shared Email ID mails

Open rajeevdave opened this issue 4 years ago • 1 comments

I have tried all way but not getting to read shared email ID. Could you please help.

from O365 import Account, MSOffice365Protocol , MSGraphProtocol 
credentials = ('client ID', 'cllient Secret')

# the default protocol will be Microsoft Graph
# the default authentication method will be "on behalf of a user"



protocol = MSGraphProtocol(default_resource='[email protected]')


scopes_graph = protocol.get_scopes_for('mailbox_shared')

#scopes = ['https://graph.microsoft.com/Mail.ReadWrite', 'https://graph.microsoft.com/Mail.Send']

print ("1")
account = Account(credentials=credentials, protocol=protocol,auth_flow_type='credentials', tenant_id='tenent_ID')
print("2")
if account.authenticate():
   print('Authenticated!')

# now account is accesing the [email protected] in every api call.
shared_mailbox_messages = account.mailbox().get_messages()

We have already created APP and provided read access to could of email ID who has access to shared email box

Getting below Error

1 2 Authenticated! Client Error: 401 Client Error: Unauthorized for url: https://graph.microsoft.com/v1.0/users/[email protected]/messages?%24top=25 | Error Message: The token contains no permissions, or permissions can not be understood. Traceback (most recent call last): File "abcmail6.py", line 23, in shared_mailbox_messages = account.mailbox().get_messages() File "C:\Users\rd185034\AppData\Local\Programs\Python\Python37\lib\site-packages\O365\mailbox.py", line 212, in get_messages response = self.con.get(url, params=params) File "C:\Users\rd185034\AppData\Local\Programs\Python\Python37\lib\site-packages\O365\connection.py", line 808, in get return self.oauth_request(url, 'get', params=params, **kwargs) File "C:\Users\rd185034\AppData\Local\Programs\Python\Python37\lib\site-packages\O365\connection.py", line 797, in oauth_request return self._internal_request(self.session, url, method, **kwargs) File "C:\Users\rd185034\AppData\Local\Programs\Python\Python37\lib\site-packages\O365\connection.py", line 759, in _internal_request raise HTTPError('{} | Error Message: {}'.format(e.args[0], error_message), response=response) from None requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://graph.microsoft.com/v1.0/users/[email protected]/messages?%24top=25 | Error Message: The token contains no permissions, or permissions can not be understood.

rajeevdave avatar Feb 19 '21 08:02 rajeevdave

either you don't have access to '[email protected]' or you don't have the admin consent.

alejcas avatar Feb 22 '21 20:02 alejcas