Package: microsoftgraph-python
microsoftgraph-pythonMicrosoft graph API wrapper for Microsoft Graph written in Python.Before startTo 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.SeeGet access on behalf of a userBreaking changes if you're upgrading prior 1.0.0Added 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.dataattribute 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 parameterapi_versionin the client instantiation.Removed Office 365 endpoints as they were merged with the Microsoft Graph API. SeeOffice 365 APIs.New in 1.0.0You can access toRequests library's Response Objectfor e.g.r = client.users.get_me()thenr.originalor the response handled by the libraryr.data.New Response propertiesr.status_codeandr.throttling.You can passRequests library's Event Hookswith the parameterrequests_hooksin the client instantiation. If you are using Django and want to log in database every request made through this library, seedjango-requests-logger.Library can auto paginate responses. Setpaginateparameter in client initialization. Defaults toTrue.Better method docstrings and type hinting.Better library structure.Installingpip install microsoftgraph-pythonUsageClient instantiationfrom microsoftgraph.client import Client
client = Client('CLIENT_ID', 'CLIENT_SECRET', account_type='common') # by default common, thus account_type is optional parameter.OAuth 2.0Get authorization urlurl = client.authorization_url(redirect_uri, scope, state=None)Exchange the code for an access tokenresponse = client.exchange_code(redirect_uri, code)Refresh tokenresponse = client.refresh_token(redirect_uri, refresh_token)Set tokenclient.set_token(token)UsersGet meresponse = client.users.get_me()MailList messagesresponse = client.mail.list_messages()Get messageresponse = client.mail.get_message(message_id)Send maildata = {
    subject="Meet for lunch?",
    content="The new cafeteria is open.",
    content_type="text",
    to_recipients=["fannyd@contoso.onmicrosoft.com"],
    cc_recipients=None,
    save_to_sent_items=True,
}
response = client.mail.send_mail(**data)List mail foldersresponse = client.mail.list_mail_folders()Create mail folderresponse = client.mail.create_mail_folder(display_name)NotesList notebooksresponse = client.notes.list_notebooks()Get notebookresponse = client.notes.get_notebook(notebook_id)Get notebook sectionsresponse = client.notes.list_sections(notebook_id)List pagesresponse = client.notes.list_pages()Create pageresponse = client.notes.create_page(section_id, files)CalendarGet eventsresponse = client.calendar.list_events(calendar_id)Get eventresponse = client.calendar.get_event(event_id)Create calendar eventfrom 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)Get calendarsresponse = client.calendar.list_calendars()Create calendarresponse = client.calendar.create_calendar(name)ContactsGet a contactresponse = client.contacts.get_contact(contact_id)Get contactsresponse = client.contacts.list_contacts()Create contactdata = {
    "given_name": "Pavel",
    "surname": "Bansky",
    "email_addresses": [
        {
            "address": "pavelb@fabrikam.onmicrosoft.com",
            "name": "Pavel Bansky"
        }
    ],
    "business_phones": [
        "+1 732 555 0102"
    ],
    "folder_id": None,
}
response = client.contacts.create_contact(**data)Get contact foldersresponse = client.contacts.list_contact_folders()Create contact foldersresponse = client.contacts.create_contact_folder()FilesGet root itemsresponse = client.files.drive_root_items()Get root children itemsresponse = client.files.drive_root_children_items()Get specific folder itemsresponse = client.files.drive_specific_folder(folder_id)Get itemresponse = client.files.drive_get_item(item_id)Download the contents of a specific itemresponse = client.files.drive_download_contents(item_id)Upload new file# 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")Update existing file# 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")Search for filesquery = ".xlsx, .xlsm"
response = client.files.search_items(query)WorkbooksCreate session for specific itemresponse = client.workbooks.create_session(workbook_id)Refresh session for specific itemresponse = client.workbooks.refresh_session(workbook_id)Close session for specific itemresponse = client.workbooks.close_session(workbook_id)Get worksheetsresponse = client.workbooks.list_worksheets(workbook_id)Get specific worksheetresponse = client.workbooks.get_worksheet(workbook_id, worksheet_id)Add worksheetsresponse = client.workbooks.add_worksheet(workbook_id)Update worksheetresponse = client.workbooks.update_worksheet(workbook_id, worksheet_id)Get chartsresponse = client.workbooks.list_charts(workbook_id, worksheet_id)Add chartresponse = client.workbooks.add_chart(workbook_id, worksheet_id)Get tablesresponse = client.workbooks.list_tables(workbook_id)Add tableresponse = client.workbooks.add_table(workbook_id)Add column to tableresponse = client.workbooks.create_table_column(workbook_id, worksheet_id, table_id)Add row to tableresponse = client.workbooks.create_table_row(workbook_id, worksheet_id, table_id)Get table rowsresponse = client.workbooks.list_table_rows(workbook_id, table_id)Get rangeresponse = client.workbooks.get_range(workbook_id, worksheet_id)Get used rangeresponse = client.workbooks.get_used_range(workbook_id, worksheet_id)Update rangeresponse1 = 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)WebhooksCreate subscriptionresponse = client.webhooks.create_subscription(change_type, notification_url, resource, expiration_datetime, client_state=None)Renew subscriptionresponse = client.webhooks.renew_subscription(subscription_id, expiration_datetime)Delete subscriptionresponse = client.webhooks.delete_subscription(subscription_id)RequirementsrequestsTeststest/test.py