Microsoft 365 & Microsoft Graph Library for Python
client = GraphClient.with_token_interactive(
tenant_name_or_id, app_client_id, admin_principal_name
)
resource = client.service_principals.get_by_name("Microsoft Graph")
app = client.applications.get_by_app_id(app_client_id)
resource.grant_application(app, "MailboxSettings.Read").execute_query()
client = GraphClient.with_token_interactive(
tenant_name_or_id, app_client_id, admin_principal_name
)
resource = client.service_principals.get_by_name("Microsoft Graph")
app_role = "User.Read.All"
user = client.users.get_by_principal_name(test_user_principal_name)
resource.grant_delegated(app_client_id, user, app_role).execute_query()
ClientContext.with_client_certificate
methodExample:
cert_credentials = {
"tenant": tenant_name,
"client_id": client_id,
"thumbprint": cert_thumbprint,
"cert_path": "selfsignkeyenc.pem",
"passphrase": "Password",
}
ctx = ClientContext(site_url).with_client_certificate(**cert_credentials)
current_web = ctx.web.get().execute_query()
Example: create a folder with a color
ctx = ClientContext(site_url).with_credentials(user_credentials)
root_folder = ctx.web.default_document_library().root_folder
folder = root_folder.folders.add(
"archive", color_hex=FolderColors.DarkGreen
).execute_query()
Example: get Range
client = GraphClient.with_username_and_password(
tenant_name, client_id, username, password
)
drive_item = client.me.drive.root.get_by_path("Financial Report.xlsx")
worksheets = drive_item.workbook.worksheets.get().execute_query()
if len(worksheets) == 0:
sys.exit("No worksheets found")
worksheet_range = worksheets["Sheet1"].range(address="A1:B3").execute_query()
file_name
parameter in File.copyto
and File.copyto_using_path
methodsparent_folder
and parent_collection
empty when retrieving file using ctx.web.get_file_by_server_relative_url
Folder.copy_to_using_path
methodmypy
and pyright
type checkers) by @kellerzaFile.download_session
method fixGraphClient.with_client_secret
and GraphClient.with_username_and_password
methods to initialize the client, refer below examplesGraphClient
client using user namename and password flow:from office365.graph_client import GraphClient
client = GraphClient.with_username_and_password(
"contoso.onmicrosoft.com", client_id, username, password
)
GraphClient
with client secret:from office365.graph_client import GraphClient
client = GraphClient.with_client_secret("contoso.onmicrosoft.com", client_id, client_secret)