def acquire_token_by_client_credentials(): settings = load_settings() authority_url = 'https://login.microsoftonline.com/{0}'.format( settings.get('default', 'tenant')) app = msal.ConfidentialClientApplication( authority=authority_url, client_id=settings.get('client_credentials', 'client_id'), client_credential=settings.get('client_credentials', 'client_secret')) return app.acquire_token_for_client( scopes=["https://graph.microsoft.com/.default"])
def get_token_for_user(): settings = load_settings() authority_url = 'https://login.microsoftonline.com/{0}'.format( settings['default']['tenant']) auth_ctx = adal.AuthenticationContext(authority_url) token = auth_ctx.acquire_token_with_username_password( 'https://graph.microsoft.com', settings['user_credentials']['username'], settings['user_credentials']['password'], settings['client_credentials']['client_id']) return token
def acquire_token_by_username_password(): settings = load_settings() authority_url = 'https://login.microsoftonline.com/{0}'.format( settings.get('default', 'tenant')) app = msal.PublicClientApplication(authority=authority_url, client_id=settings.get( 'client_credentials', 'client_id')) result = app.acquire_token_by_username_password( username=settings.get('user_credentials', "username"), password=settings.get('user_credentials', "password"), scopes=["https://graph.microsoft.com/.default"]) return result
from examples import acquire_token_client_credentials from office365.graph_client import GraphClient from tests import load_settings def upload_files(remote_drive, local_root_path): """ Uploads files from local folder into OneDrive drive :type remote_drive: Drive :type local_root_path: str """ for name in os.listdir(local_root_path): path = join(local_root_path, name) if isfile(path): with open(path, 'rb') as local_file: content = local_file.read() uploaded_drive_item = remote_drive.root.upload( name, content).execute_query() print( "File '{0}' uploaded into {1}".format( path, uploaded_drive_item.web_url), ) settings = load_settings() client = GraphClient(acquire_token_client_credentials) user_name = settings.get('test_alt_account_name') target_drive = client.users[user_name].drive # get target drive # import local files into OneDrive upload_files(target_drive, "../data")