Skip to content

Instantly share code, notes, and snippets.

@frennkie
Created February 4, 2019 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frennkie/e8d3cd0fd90be6338ea5bd8c79b5c2e3 to your computer and use it in GitHub Desktop.
Save frennkie/e8d3cd0fd90be6338ea5bd8c79b5c2e3 to your computer and use it in GitHub Desktop.
from O365 import Account, Connection, FileSystemTokenBackend, MSGraphProtocol, MSOffice365Protocol
import re
client_id = "<client_id>"
client_secret = "<client_secret>"
tenant_id = "<tenant_id>"
credentials = (client_id, client_secret)
token_backend = FileSystemTokenBackend(token_path='.', token_filename='my_o365_token.txt')
account = Account(credentials, token_backend=token_backend)
scopes = ['basic', 'message_all']
redirect_uri = "https://outlook.office365.com/mail/inbox"
protocol_graph = MSGraphProtocol()
scopes_graph = protocol_graph.get_scopes_for(scopes)
account.con = Connection(credentials, scopes=scopes_graph, token_backend=token_backend, tenant_id=tenant_id)
account.con.get_authorization_url(redirect_uri=redirect_uri)
# Use a Browser and path the result of last command
# which should be someting like: 'https://login.micros...
# After signing and granting access rights you will be sent to
# https://outlook.office365.com/mail/inbox and the URL should
# also include "code=.."
# Take the full url and add it the command below
result_url = re.sub('#', '?', '<insert_url_here>')
# now you can request a token (which will be valid for 1 hour)
account.con.request_token(result_url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment