Microsoft graph API wrapper for Microsoft Graph written in Python.
To use Microsoft Graph to read and write resources on behalf of a user, your app must get an access token from the Microsoft identity platform and attach the token to requests that it sends to Microsoft Graph. The exact authentication flow that you will use to get access tokens will depend on the kind of app you are developing and whether you want to use OpenID Connect to sign the user in to your app. One common flow used by native and mobile apps and also by some Web apps is the OAuth 2.0 authorization code grant flow.
See Get access on behalf of a user
- Added structure to library to match API documentation for e.g.
client.get_me()
=>client.users.get_me()
. - Renamed several methods to match API documentation for e.g.
client.get_me_events()
=>client.calendar.list_events()
. - Result from calling a method is not longer a dictionary but a Response object. To access the dict response as before then call
.data
attribute for e.gr = client.users.get_me()
thenr.data
. - Previous API calls made through beta endpoints are now pointing to v1.0 by default. This can be changed to beta if needed with the parameter
api_version
in the client instantiation. - Removed Office 365 endpoints as they were merged with the Microsoft Graph API. See Office 365 APIs.
- You can access to Requests library's Response Object for e.g.
r = client.users.get_me()
thenr.original
or the response handled by the libraryr.data
. - New Response properties
r.status_code
andr.throttling
. - You can pass Requests library's Event Hooks with the parameter
requests_hooks
in the client instantiation. If you are using Django and want to log in database every request made through this library, see django-requests-logger. - Library can auto paginate responses. Set
paginate
parameter in client initialization. Defaults toTrue
. - Better method docstrings and type hinting.
- Better library structure.
pip install microsoftgraph-python
from microsoftgraph.client import Client
client = Client('CLIENT_ID', 'CLIENT_SECRET', account_type='common') # by default common, thus account_type is optional parameter.
url = client.authorization_url(redirect_uri, scope, state=None)
response = client.exchange_code(redirect_uri, code)
response = client.refresh_token(redirect_uri, refresh_token)
client.set_token(token)
response = client.users.get_me()
response = client.mail.list_messages()
response = client.mail.get_message(message_id)
data = {
subject="Meet for lunch?",
content="The new cafeteria is open.",
content_type="text",
to_recipients=["[email protected]"],
cc_recipients=None,
save_to_sent_items=True,
}
response = client.mail.send_mail(**data)
response = client.mail.list_mail_folders()
response = client.mail.create_mail_folder(display_name)
response = client.notes.list_notebooks()
response = client.notes.get_notebook(notebook_id)
response = client.notes.list_sections(notebook_id)
response = client.notes.list_pages()
response = client.notes.create_page(section_id, files)
response = client.calendar.list_events(calendar_id)
response = client.calendar.get_event(event_id)
from datetime import datetime, timedelta
start_datetime = datetime.now() + timedelta(days=1) # tomorrow
end_datetime = datetime.now() + timedelta(days=1, hours=1) # tomorrow + one hour
timezone = "America/Bogota"
data = {
"calendar_id": "CALENDAR_ID",
"subject": "Let's go for lunch",
"content": "Does noon work for you?",
"content_type": "text",
"start_datetime": start_datetime,
"start_timezone": timezone,
"end_datetime": end_datetime,
"end_timezone": timezone,
"location": "Harry's Bar",
}
response = client.calendar.create_event(**data)
response = client.calendar.list_calendars()
response = client.calendar.create_calendar(name)
response = client.contacts.get_contact(contact_id)
response = client.contacts.list_contacts()
data = {
"given_name": "Pavel",
"surname": "Bansky",
"email_addresses": [
{
"address": "[email protected]",
"name": "Pavel Bansky"
}
],
"business_phones": [
"+1 732 555 0102"
],
"folder_id": None,
}
response = client.contacts.create_contact(**data)
response = client.contacts.list_contact_folders()
response = client.contacts.create_contact_folder()
response = client.files.drive_root_items()
response = client.files.drive_root_children_items()
response = client.files.drive_specific_folder(folder_id)
response = client.files.drive_get_item(item_id)
response = client.files.drive_download_contents(item_id)
# This example uploads the image in path to a file in the signed-in user's drive under Pictures named upload.jpg.
response = client.files.drive_upload_new_file("/Pictures/upload.jpg", "/mnt/c/Users/i/Downloads/image1.jpg")
# This example uploads the image in path to update an existing item id.
response = client.files.drive_update_existing_file(item_id, "/mnt/c/Users/i/Downloads/image2.jpg")
query = ".xlsx, .xlsm"
response = client.files.search_items(query)
response = client.workbooks.create_session(workbook_id)
response = client.workbooks.refresh_session(workbook_id)
response = client.workbooks.close_session(workbook_id)
response = client.workbooks.list_worksheets(workbook_id)
response = client.workbooks.get_worksheet(workbook_id, worksheet_id)
response = client.workbooks.add_worksheet(workbook_id)
response = client.workbooks.update_worksheet(workbook_id, worksheet_id)
response = client.workbooks.list_charts(workbook_id, worksheet_id)
response = client.workbooks.add_chart(workbook_id, worksheet_id)
response = client.workbooks.list_tables(workbook_id)
response = client.workbooks.add_table(workbook_id)
response = client.workbooks.create_table_column(workbook_id, worksheet_id, table_id)
response = client.workbooks.create_table_row(workbook_id, worksheet_id, table_id)
response = client.workbooks.list_table_rows(workbook_id, table_id)
response = client.workbooks.get_range(workbook_id, worksheet_id)
response = client.workbooks.get_used_range(workbook_id, worksheet_id)
response1 = client.workbooks.create_session(workbook_id)
workbook_session_id = response1.data["id"]
client.set_workbook_session_id(workbook_session_id)
range_address = "A1:D2"
data = {
"values": [
["John", "Doe", "+1 305 1234567", "Miami, FL"],
["Bill", "Gates", "+1 305 1234567", "St. Redmond, WA"],
]
}
response2 = client.workbooks.update_range(workbook_id, worksheet_id, range_address, json=data)
response3 = client.worbooks.close_session(workbook_id)
response = client.webhooks.create_subscription(change_type, notification_url, resource, expiration_datetime, client_state=None)
response = client.webhooks.renew_subscription(subscription_id, expiration_datetime)
response = client.webhooks.delete_subscription(subscription_id)
- requests
test/test.py