from office365.graph.graph_client import GraphClient
from settings import settings


def get_token_for_user(auth_ctx):
    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 enum_folders_and_files(root_folder):
    drive_items = root_folder.children
    client.load(drive_items)
    client.execute_query()
    for drive_item in drive_items:
        item_type = drive_item.folder.is_server_object_null and "file" or "folder"
        print("Type: {0} Name: {1}".format(item_type, drive_item.name))
        if not drive_item.folder.is_server_object_null and drive_item.folder.childCount > 0:
            enum_folders_and_files(drive_item)


client = GraphClient(settings['tenant'], get_token_for_user)
root = client.me.drive.root
enum_folders_and_files(root)

def download_files(remote_folder, local_path):
    drive_items = remote_folder.children
    client.load(drive_items)
    client.execute_query()
    for drive_item in drive_items:
        if not drive_item.file.is_server_object_null:  # is file?
            # download file content
            with open(os.path.join(local_path, drive_item.name),
                      'wb') as local_file:
                drive_item.download(local_file)
                client.execute_query()
            print("File '{0}' has been downloaded".format(local_file.name))


# example demonstrates how to export OneDrive files into local file system

# connect
client = GraphClient(settings['tenant'], get_token)

# load drive properties
drive = client.users["*****@*****.**"].drive
client.load(drive)
client.execute_query()

# download files from OneDrive
with tempfile.TemporaryDirectory() as path:
    download_files(drive.root, path)
    print("Done")
 def setUpClass(cls):
     cls.client = GraphClient(settings['tenant'], get_token)
        settings['client_credentials']['client_id'])
    return token


def generate_user_profile():
    fake = Faker()

    user_json = {
        'givenName': fake.name(),
        'companyName': fake.company(),
        'businessPhones': [fake.phone_number()],
        'officeLocation': fake.street_address(),
        'city': fake.city(),
        'country': fake.country(),
        'principalName': "{0}@{1}".format(fake.user_name(),
                                          settings['tenant']),
        'password': "******".format(random_seed),
        'accountEnabled': True
    }
    return UserProfile(**user_json)


client = GraphClient(settings['tenant'], acquire_token)

for idx in range(0, 5):
    user_profile = generate_user_profile()
    user = client.users.add(user_profile)
    client.execute_query()
    print("{0} user has been created".format(
        user.properties['userPrincipalName']))
Example #5
0
from office365.graph.graph_client import GraphClient
from settings import settings


def get_token_for_user(auth_ctx):
    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


client = GraphClient(settings['tenant'], get_token_for_user)

groups = client.groups
client.load(groups)
client.execute_query()
no = 1
groups_count = len(groups)
for grp in groups:
    print("({0} of {1}) Deleting {2} group ...".format(
        no, groups_count, grp.properties['displayName']))
    # 1st step: delete group
    grp.delete_object()
    client.execute_query()

    # 2nd step: permanently delete (deleted) group
    deleted_group = client.directory.deletedGroups[grp.id]
    deleted_group.delete_object()
    client.execute_query()
        'https://graph.microsoft.com',
        settings['user_credentials']['username'],
        settings['user_credentials']['password'],
        settings['client_credentials']['client_id'])
    return token


def create_group_for_team(groups, name):
    grp_properties = GroupProfile(name)
    grp_properties.securityEnabled = False
    grp_properties.mailEnabled = True
    grp_properties.groupTypes = ["Unified"]
    target_group = groups.add(grp_properties)
    return target_group


def print_failure(retry_number):
    print(f"{retry_number}: trying to create a team...")


client = GraphClient(settings['tenant'], acquire_token)

group_name = "Team_" + uuid.uuid4().hex
result = client.teams.create(group_name)
client.execute_query_retry(max_retry=5, on_failure=print_failure)
print("Team has been provisioned")

channels = result.value.channels
client.load(channels)
client.execute_query()