What are the steps to dynamically refresh your token to get mailbox messages in a daily basis (>90 days)?
Hey everyone, first of all, thanks for this library
I've been looking at the GitHub issues and did not find a full solution for this particular use case,
These are the steps I've done so far by following the README.
-
Created the app in the Application Registration Portal and followed the instructions, I ended up with this

-
I authenticated for the first time and was able to generate the token (downloaded into my local directory). This is how I did it:
self.auth = (self.config.get('client_id'), self.config.get('client_secret'))
self.account = O365.Account(credentials=self.auth)
scope_helpers = ['https://graph.microsoft.com/Mail.ReadWrite']
result = self.account.authenticate(scopes=scope_helpers)
- The Python Terminal returned the url to give consent and clicked on it
- I was able to sign in and copy the generated url to paste it under "Paste the authenticated url here:"
- Voila, I was able to get access to the mailbox and get the messages
self.inbox = self.account.mailbox().inbox_folder()
unfiltered_messages = self.inbox.get_messages(query=query, limit=self.message_search_count)
# more logic here to get the attachments from emails
- After making sure that my code was doing what I wanted (download attachments from emails), I waited for one hour for the token to get expired.
- I ran the same code without the authenticate method and got the following error:
{'error': 'invalid_request', 'error_description': "AADSTS900144: The request body must contain the following parameter: 'refresh_token'.\........ more traceback metadasta'} - After reading the GitHub issues I realized that I did not have
offline_accessas part of my scope and that might've been the problem. So I added it to authenticate again for the first time.
self.auth = (self.config.get('client_id'), self.config.get('client_secret'))
self.account = O365.Account(credentials=self.auth)
# This is what I modified
scope_helpers = ['https://graph.microsoft.com/Mail.ReadWrite', 'offline_access']
result = self.account.authenticate(scopes=scope_helpers)
But got the following...

And this is where I'm stuck, I double checked my app registration and looks like offline_access is granted, I'm sure I don't understand how the API works, what would be the steps to accomplish the use case mentioned in the title of this issue?
Thanks
Scope helpers are not raw scopes. Just strings, goto the connection.py file and look for the variable DEFAULT_SCOPES. There you have what strings can you pass to account.authenticate and what this strings translate to.
@janscas thanks Alejandro, let me confirm if I understand your suggestion, after your suggestion I assumed that I could do this to use the actual defined scopes:
protocol = MSGraphProtocol()
scope_helpers = protocol.get_scopes_for(['message_all', 'basic'])
self.account.authenticate(scopes=scope_helpers)
And I still got the error when trying to give consent:
AADSTS70011: The provided value for the input parameter 'scope' is not valid. The scope https://graph.microsoft.com/User.Read https://graph.microsoft.com/Mail.Send https://graph.microsoft.com/Mail.ReadWrite https://graph.microsoft.com/offline_access is not valid. The resources must specify a known valid scope..
My bad, I got it, instead of using the scope raw values ('https://graph.microsoft.com/Mail.ReadWrite', etc), we have to use the pre-defined scope keys or definitions ('message_all', 'basic', 'message_send'):
scopes = ['message_all', 'basic']
result = self.account.authenticate(scopes=scopes)
After doing that i was able to re-generate the token with the refresh_token attribute :) thanks a lot.
{
"token_type": "Bearer",
"scope": [
"profile",
"openid",
"email",
"https://graph.microsoft.com/Mail.ReadWrite",
"https://graph.microsoft.com/Mail.Send",
"https://graph.microsoft.com/User.Read"
],
"expires_in": 3600,
"ext_expires_in": 3600,
"access_token": "XXXXXXXXXXXX",
"refresh_token": "XXXXXXXX",
"expires_at": 1547754323.767873
}
That was the misunderstanding, thanks man