Skip to main content

An easy interface for Microsoft Office 365 Oauth Device Code authentication.

Project description

An easy interface for Microsoft Office 365 Oauth Device Code authentication.


pip install oauth_tools


from oauth_tools import AuthManager
import webbrowser
import creds
import os

if not os.path.exists('users.json'):
    open('users.json', mode='wt').write(json.dumps({}))

authManager = AuthManager(
    # Get your client/tenant ID by following these instructions:

authManager.SaveIncompleteOACallback = lambda ID, data: open('incomplete.json', mode='wt').write(
    json.dumps({ID: data}, indent=2))
authManager.GetIncompleteOACallback = lambda ID: json.loads(open('incomplete.json', mode='rt').read()).get(ID, None)

authManager.SaveToDBCallback = lambda ID, data: open('users.json', mode='wt').write(
    json.dumps({ID: data}, indent=2))
authManager.GetFromDBCallback = lambda ID: json.loads(open('users.json', mode='rt').read()).get(ID, None)

MY_ID = 'Grant'

user = authManager.GetUserByID(MY_ID)
if user is None:
    # the user has not authenticated before
    d = authManager.CreateNewUser(MY_ID)['verification_uri'])
    print('Enter the code', d['user_code'])

    while True:
        status = authManager.CheckOAStatus(MY_ID)
        print('status=', status)
        if status == 'Success':

    user = authManager.GetUserByID(MY_ID)

print('user=', user)

The output looks like

>>> Enter the code A4S4QS4EG
>>> status= Waiting for user to authenticate
>>> status= Success
>>> user= <User: ID=Grant,, AccessToken=abcdefghijklm...>

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for oauth-tools, version 0.0.2
Filename, size File type Python version Upload date Hashes
Filename, size oauth_tools-0.0.2-py3-none-any.whl (6.2 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size oauth_tools-0.0.2.tar.gz (5.9 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page