python-o365
python-o365 copied to clipboard
Usage for non-public 365 site
I have been trying (unsuccessfully) to use the basic requests_oauthlib) to connect to our Office 365 site.
The login URL is: LoginURL = 'https://login.microsoftonline.us/' which I don't see how to provide in your Account() method. Is there a way to do this?
Here is our current code to get the token:"
def getToken(store):
""" Get OAUTH Token and session"""
tokenURL = LoginURL + TenantID + '/oauth2/v2.0/token'
scope = "https://dod-graph.microsoft.us/.default"
client = oauthlib.oauth2.BackendApplicationClient(client_id=ClientID, scope=scope)
session = requests_oauthlib.OAuth2Session(client)
session.verify = False
secret = store['secret']
print(f"--token URL: {tokenURL}")
token = session.fetch_token(token_url=tokenURL, client_id=ClientID, client_secret=secret)
print(f"--token: {token}")
return session
The above code complains there is no "code" parameter (which sounds silly, since this is a backend app and this is the request to get the initial token).
Requests will be against the GRAPH API reports section:
https://learn.microsoft.com/en-us/graph/api/reportroot-getmailboxusagedetail?view=graph-rest-beta#code-try-1
Does anyone maintain this project anymore?
I ask because I do think it has some value, but as you can see from the above, it is hard to get a simple thing such as fetching the token. In 20 minutes, I got this to work with the vanilla python requests library:
def getToken(store):
""" Get OAUTH Token and session"""
global Token
tokenURL = LoginURL + TenantID + '/oauth2/v2.0/token'
session = requests.Session()
session.verify = False
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
body = {
"client_id": ClientID,
"scope": "https://dod-graph.microsoft.us/.default",
"username": TenantID,
"password": store['secret'],
"grant_type": "client_credentials",
"client_secret": {store['secret']}
}
response = session.post(tokenURL, data=body, headers=headers)
reply = response.json()
token = reply['access_token']
print(f"--reply: {reply}")
headers = {'Authorization': f'Bearer {token}', 'Content-Type': 'application/json'}
session.headers.update(headers)
OToken = token
return session
Account.con.oauth_redirect_url