Reputation: 183
I have a script to do a web scrape and send the data in an email via the O365 package. I've registered the app with Azure, and completed the authentication flow and the code runs fine in Jupyter Notebook. However, problems begin when I try to run the script via Task Scheduler or in a batch file. Here is the code:
content = html_template.format(df.to_html(index=False))
credentials = ('AzureappID', 'azureSecret')
account = Account(credentials)
m = account.new_message()
m.to.add('[email protected]')
m.subject = 'Test'
m.body = content
m.send()
The batch file shows me this error, which appears to occur on the m.send()
line:
File "C:\ProgramData\Anaconda3\lib\site-packages\O365\message.py", line 694, in send
response = self.con.post(url, data=data)
File "C:\ProgramData\Anaconda3\lib\site-packages\O365\connection.py", line 787, in post
return self.oauth_request(url, 'post', data=data, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\O365\connection.py", line 763, in oauth_request
self.session = self.get_session(load_token=True)
File "C:\ProgramData\Anaconda3\lib\site-packages\O365\connection.py", line 539, in get_session
raise RuntimeError('No auth token found. Authentication Flow needed')
RuntimeError: No auth token found. Authentication Flow needed
It appears to say the token has been loaded, but then in the "get_session" line says no token has been found. As I said, the code runs fine in Jupyter - it seems as though task scheduler/the batch file cannot find the token. I've seen here that the token may need to be saved in a root directory, but I'm not sure where the token is stored, or what the appropriate root directory would be for storage.
Any thoughts on this auth flow?
Upvotes: 1
Views: 3227
Reputation: 51
I had the same issue. Per the docs, the user should use FileSystemTokenBackend. I ended up creating a file named 'o365_token.txt' and storing this in the same folder as my script. I believe this is the default name of the file once you create the token with FileSystemTokenBackend. O365 Documentation FileSystemTokenBackend
Upvotes: 1