Office365-REST-Python-Client


NameOffice365-REST-Python-Client JSON
Version 2.5.8 PyPI version JSON
download
home_pagehttps://github.com/vgrem/Office365-REST-Python-Client
SummaryMicrosoft 365 & Microsoft Graph Library for Python
upload_time2024-04-14 16:11:11
maintainerKonrad GÄ…dek, Domenico Di Nicola
docs_urlNone
authorVadim Gremyachev
requires_pythonNone
licenseMIT
keywords git
VCS
bugtrack_url
requirements requests requests_ntlm setuptools msal pytz typing_extensions
Travis-CI
coveralls test coverage No coveralls.
            # About
Microsoft 365 & Microsoft Graph library for Python

# Usage

1. [Installation](#Installation)
2. [Working with SharePoint API](#Working-with-SharePoint-API) 
3. [Working with Outlook API](#Working-with-Outlook-API) 
4. [Working with OneDrive and SharePoint API v2 APIs](#working-with-onedrive-and-sharepoint-v2-apis)
5. [Working with Teams API](#Working-with-Microsoft-Teams-API)
6. [Working with OneNote API](#Working-with-Microsoft-OneNote-API)
7. [Working with Planner API](#Working-with-Microsoft-Planner-API)  

## Status
[![Downloads](https://pepy.tech/badge/office365-rest-python-client/month)](https://pepy.tech/project/office365-rest-python-client)
[![PyPI](https://img.shields.io/pypi/v/Office365-REST-Python-Client.svg)](https://pypi.python.org/pypi/Office365-REST-Python-Client)
[![PyPI pyversions](https://img.shields.io/pypi/pyversions/Office365-REST-Python-Client.svg)](https://pypi.python.org/pypi/Office365-REST-Python-Client/)
[![Build Status](https://travis-ci.com/vgrem/Office365-REST-Python-Client.svg?branch=master)](https://travis-ci.com/vgrem/Office365-REST-Python-Client)

# Installation

Use pip:

```
pip install Office365-REST-Python-Client
```

### Note 
>
>Alternatively the _latest_ version could be directly installed via GitHub:
>```
>pip install git+https://github.com/vgrem/Office365-REST-Python-Client.git
>```

# Authentication
For the following examples, relevant credentials can be found in the Azure Portal.

Steps to access:
1. Login to the home page of the Azure Portal
2. Navigate to "Azure Active Directory" using the three bars in the top right corner of the portal
3. Select "App registrations" in the navigation panel on the left
4. Search for and select your relevant application
5. In the application's "Overview" page, the client id can be found under "Application (client) id"
6. In the application's "Certificates & Secrets" page, the client secret can be found under the "Value" of the "Client Secrets." If there is no client secret yet, create one here.


# Working with SharePoint API

   The `ClientContext` client provides the support for a legacy SharePoint REST and OneDrive for Business REST APIs, 
   the list of supported versions: 
   -   [SharePoint 2013 REST API](https://msdn.microsoft.com/en-us/library/office/jj860569.aspx) and above 
   -   [SharePoint Online REST API](https://learn.microsoft.com/en-us/sharepoint/dev/sp-add-ins/get-to-know-the-sharepoint-rest-service)
   -   OneDrive for Business REST API

### Authentication

   The following auth flows are supported:

#### 1. Using a SharePoint App-Only principal (client credentials flow)

   This auth method is compatible with SharePoint on-premises and still relevant 
   model in both SharePoint on-premises as SharePoint Online, 
   the following methods are available: 

   - `ClientContext.with_credentials(client_credentials)` 
   - `ClientContext.with_client_credentials(client_id, client_secret)`
  
   Usage:
   ``` 
   client_credentials = ClientCredential('{client_id}','{client_secret}')
   ctx = ClientContext('{url}').with_credentials(client_credentials)
   ```
  
   Documentation:
   - [Granting access using SharePoint App-Only](https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azureacs)  
   - [wiki](https://github.com/vgrem/Office365-REST-Python-Client/wiki/How-to-connect-to-SharePoint-Online-and-and-SharePoint-2013-2016-2019-on-premises--with-app-principal)
  
   Example: [connect_with_app_principal.py](examples/sharepoint/auth_app_only.py)
  
#### 2. Using username and password 

   Usage:
   ``` 
   user_credentials = UserCredential('{username}','{password}')
   ctx = ClientContext('{url}').with_credentials(user_credentials)
   ```
  
   Example: [connect_with_user_credential.py](examples/sharepoint/auth_user_credential.py)
  
#### 3. Using an Azure AD application (certificate credentials flow) 

  Documentation: 
   - [Granting access via Azure AD App-Only](https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azuread)  
   - [wiki](https://github.com/vgrem/Office365-REST-Python-Client/wiki/How-to-connect-to-SharePoint-Online-with-certificate-credentials) 
  
  Example: [connect_with_client_certificate.py](examples/sharepoint/auth_client_certificate.py)

#### 4. Interactive

   to login interactively i.e. via a local browser

   Prerequisite: 
   
   > In Azure Portal, configure the Redirect URI of your
   "Mobile and Desktop application" as ``http://localhost``.

  Example: [connect_interactive.py](examples/sharepoint/auth_interactive.py)

  Usage:
```python
from office365.sharepoint.client_context import ClientContext
ctx = ClientContext(site_url).with_interactive(tenant_name_or_id, client_id)
me = ctx.web.current_user.get().execute_query()
print(me.login_name)
```

### Examples
 
There are **two approaches** available to perform API queries:

1. `ClientContext class` - where you target SharePoint resources such as `Web`, `ListItem` and etc (recommended)
 
```python
from office365.runtime.auth.user_credential import UserCredential
from office365.sharepoint.client_context import ClientContext
site_url = "https://{your-tenant-prefix}.sharepoint.com"
ctx = ClientContext(site_url).with_credentials(UserCredential("{username}", "{password}"))
web = ctx.web
ctx.load(web)
ctx.execute_query()
print("Web title: {0}".format(web.properties['Title']))
```
or alternatively via method chaining (a.k.a Fluent Interface): 

```python
from office365.runtime.auth.user_credential import UserCredential
from office365.sharepoint.client_context import ClientContext
site_url = "https://{your-tenant-prefix}.sharepoint.com"
ctx = ClientContext(site_url).with_credentials(UserCredential("{username}", "{password}"))
web = ctx.web.get().execute_query()
print("Web title: {0}".format(web.properties['Title']))
```


2. `RequestOptions class` - where you construct REST queries (and no model is involved)

   The example demonstrates how to read `Web` properties:
   
```python
import json
from office365.runtime.auth.user_credential import UserCredential
from office365.runtime.http.request_options import RequestOptions
from office365.sharepoint.client_context import ClientContext
site_url = "https://{your-tenant-prefix}.sharepoint.com"
ctx = ClientContext(site_url).with_credentials(UserCredential("{username}", "{password}"))
request = RequestOptions("{0}/_api/web/".format(site_url))
response = ctx.pending_request().execute_request_direct(request)
json = json.loads(response.content)
web_title = json['d']['Title']
print("Web title: {0}".format(web_title))
```

The list of examples:

- Working with files
  - [download a file](examples/sharepoint/files/download.py) 
  - [upload a file](examples/sharepoint/files/upload.py)

- Working with lists and list items
  -  [create a list item](examples/sharepoint/lists/data_generator.py)
  -  [read a list item](examples/sharepoint/lists/read_paged.py)   
  -  [update a list item](examples/sharepoint/listitems/update_batch.py)
  -  [delete a list item](examples/sharepoint/listitems/delete.py) 
  
Refer [examples section](examples/sharepoint) for another scenarios

### Support for non-standard SharePoint Online Environments

  Support for non-standard SharePoint Environments is currently being implemented. Currently supported:
  - GCC High

  To enable authentication to GCC High endpoints, add the `environment='GCCH'` parameter when calling the 
  `ClientContext class` with `.with_user_credentials`, `.with_client_credentials`, or `.with_credentials`

   Example:
   ```python
   client_credentials = ClientCredential('{client_id}','{client_secret}')
   ctx = ClientContext('{url}').with_credentials(client_credentials, environment='GCCH')
   ```

# Working with Outlook API

The list of supported APIs:
-   [Outlook Contacts REST API](https://msdn.microsoft.com/en-us/office/office365/api/contacts-rest-operations)
-   [Outlook Calendar REST API](https://msdn.microsoft.com/en-us/office/office365/api/calendar-rest-operations)
-   [Outlook Mail REST API](https://msdn.microsoft.com/en-us/office/office365/api/mail-rest-operations)


Since Outlook REST APIs are available in both Microsoft Graph and the Outlook API endpoint, 
the following clients are available:

- `GraphClient` which targets Outlook API `v2.0` version (*preferable* nowadays, refer [transition to Microsoft Graph-based Outlook REST API](https://docs.microsoft.com/en-us/outlook/rest/compare-graph-outlook) for a details)   
~~- `OutlookClient` which targets Outlook API `v1.0` version (not recommended for usage since `v1.0` version is being deprecated.)~~


### Authentication

[The Microsoft Authentication Library (MSAL) for Python](https://pypi.org/project/msal/) which comes as a dependency 
is used as a default library to obtain tokens to call Microsoft Graph API. 

Using [Microsoft Authentication Library (MSAL) for Python](https://pypi.org/project/msal/)

> Note: access token is getting acquired via [Client Credential flow](https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-client-creds-grant-flow)
> in the provided examples. Other forms of token acquisition can be found here: https://msal-python.readthedocs.io/en/latest/

```python
import msal
from office365.graph_client import GraphClient

def acquire_token():
    """
    Acquire token via MSAL
    """
    authority_url = 'https://login.microsoftonline.com/{tenant_id_or_name}'
    app = msal.ConfidentialClientApplication(
        authority=authority_url,
        client_id='{client_id}',
        client_credential='{client_secret}'
    )
    token = app.acquire_token_for_client(scopes=["https://graph.microsoft.com/.default"])
    return token


client = GraphClient(acquire_token)

```

But in terms of Microsoft Graph API authentication, another OAuth spec compliant libraries 
such as [adal](https://github.com/AzureAD/azure-activedirectory-library-for-python) 
are supported as well.   

Using [ADAL Python](https://adal-python.readthedocs.io/en/latest/#)

Usage

```python
import adal
from office365.graph_client import GraphClient

def acquire_token_func():
    authority_url = 'https://login.microsoftonline.com/{tenant_id_or_name}'
    auth_ctx = adal.AuthenticationContext(authority_url)
    token = auth_ctx.acquire_token_with_client_credentials(
        "https://graph.microsoft.com",
        "{client_id}",
        "{client_secret}")
    return token

client = GraphClient(acquire_token_func)

```

#### Example

The example demonstrates how to send an email via [Microsoft Graph endpoint](https://docs.microsoft.com/en-us/graph/api/user-sendmail?view=graph-rest-1.0&tabs=http).

> Note: access token is getting acquired  via [Client Credential flow](https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-client-creds-grant-flow)

```python
from office365.graph_client import GraphClient

client = GraphClient(acquire_token_func)

client.me.send_mail(
    subject="Meet for lunch?",
    body="The new cafeteria is open.",
    to_recipients=["fannyd@contoso.onmicrosoft.com"]
).execute_query()

```


Additional examples & scenarios:

-  [download a message](examples/outlook/messages/download.py) 
-  [list messages](examples/outlook/messages/list_all.py)
-  [move messages to a different folder](examples/outlook/messages/move.py)
-  [search messages](examples/outlook/messages/search.py)   
-  [send messages](examples/outlook/messages/send.py)
-  [send messages with attachments](examples/outlook/messages/send_with_attachment.py) 
-  [enable sending emails on behalf of another user in your organization](https://learn.microsoft.com/en-us/microsoft-365/solutions/allow-members-to-send-as-or-send-on-behalf-of-group)

Refer to [examples section](examples/outlook) for other scenarios


# Working with OneDrive and SharePoint v2 APIs

#### Documentation 

[OneDrive Graph API reference](https://docs.microsoft.com/en-us/graph/api/resources/onedrive?view=graph-rest-1.0)

#### Authentication

[The Microsoft Authentication Library (MSAL) for Python](https://pypi.org/project/msal/) which comes as a dependency 
is used to obtain token

```python
import msal

def acquire_token_func():
    """
    Acquire token via MSAL
    """
    authority_url = 'https://login.microsoftonline.com/{tenant_id_or_name}'
    app = msal.ConfidentialClientApplication(
        authority=authority_url,
        client_id='{client_id}',
        client_credential='{client_secret}'
    )
    token = app.acquire_token_for_client(scopes=["https://graph.microsoft.com/.default"])
    return token
``` 


#### Examples 

##### Example: list available drives

The example demonstrates how to enumerate and print drive's url 
which corresponds to [`list available drives` endpoint](https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/drive_list?view=odsp-graph-online)

> Note: access token is getting acquired  via [Client Credential flow](https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-client-creds-grant-flow)

```python
from office365.graph_client import GraphClient

tenant_name = "contoso.onmicrosoft.com"
client = GraphClient(acquire_token_func)
drives = client.drives.get().execute_query()
for drive in drives:
    print("Drive url: {0}".format(drive.web_url))
```


##### Example: download the contents of a DriveItem(folder facet)

```python
from office365.graph_client import GraphClient
client = GraphClient(acquire_token_func)
# retrieve drive properties 
drive = client.users["{user_id_or_principal_name}"].drive.get().execute_query()
# download files from OneDrive into local folder 
with tempfile.TemporaryDirectory() as path:
    download_files(drive.root, path)
```

where

```python
def download_files(remote_folder, local_path):
    drive_items = remote_folder.children.get().execute_query()
    for drive_item in drive_items:
        if drive_item.file is not None:  # is file?
            # download file content
            with open(os.path.join(local_path, drive_item.name), 'wb') as local_file:
                drive_item.download(local_file).execute_query()
```

Additional examples:

-  [create list column](examples/onedrive/columns/create_text.py) 
-  [download file](examples/onedrive/files/download.py)
-  [export files](examples/onedrive/files/export.py)
-  [upload folder](examples/onedrive/folders/upload.py)   
-  [list drives](examples/onedrive/drives/list.py)
-  [list files](examples/onedrive/folders/list_files.py)

Refer to [OneDrive examples section](examples/onedrive) for more examples.


# Working with Microsoft Teams API

#### Authentication

[The Microsoft Authentication Library (MSAL) for Python](https://pypi.org/project/msal/) which comes as a dependency 
is used to obtain token  

#### Examples 

##### Example: create a new team under a group

The example demonstrates how create a new team under a group 
which corresponds to [`Create team` endpoint](https://docs.microsoft.com/en-us/graph/api/team-put-teams?view=graph-rest-1.0&tabs=http)

```python
from office365.graph_client import GraphClient
client = GraphClient(acquire_token_func)
new_team = client.groups["{group_id}"].add_team().execute_query_retry()
```

Additional examples:

-  [create a team](examples/teams/create_team.py) 
-  [create team from group](examples/teams/create_from_group.py)
-  [list all teams](examples/teams/list_all.py)
-  [list my teams](examples/teams/list_my_teams.py)   
-  [send messages](examples/teams/send_message.py)
  
Refer to [examples section](examples/teams) for other scenarios

# Working with Microsoft Onenote API

The library supports OneNote API in terms of calls to a user's OneNote notebooks, sections, and pages in a personal or organization account

Example: Create a new page

```python
from office365.graph_client import GraphClient
client = GraphClient(acquire_token_func)

files = {}
with open("./MyPage.html", 'rb') as f, \
    open("./MyImage.png", 'rb') as img_f, \
    open("./MyDoc.pdf", 'rb') as pdf_f:
    files["imageBlock1"] = img_f
    files["fileBlock1"] = pdf_f
    page = client.me.onenote.pages.add(presentation_file=f, attachment_files=files).execute_query()
```

# Working with Microsoft Planner API

The example demonstrates how to create a new planner task
which corresponds to [`Create plannerTask` endpoint](https://docs.microsoft.com/en-us/graph/api/planner-post-tasks?view=graph-rest-1.0):

```python
from office365.graph_client import GraphClient

client = GraphClient(acquire_token_func)
task = client.planner.tasks.add(title="New task", planId="--plan id goes here--").execute_query()
```

# Third Party Libraries and Dependencies
The following libraries will be installed when you install the client library:
* [requests](https://github.com/kennethreitz/requests)
* [Microsoft Authentication Library (MSAL) for Python](https://pypi.org/project/msal/)




            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/vgrem/Office365-REST-Python-Client",
    "name": "Office365-REST-Python-Client",
    "maintainer": "Konrad G\u0105dek, Domenico Di Nicola",
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": "kgadek@gmail.com, dom.dinicola@gmail.com",
    "keywords": "git",
    "author": "Vadim Gremyachev",
    "author_email": "vvgrem@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/a1/2c/71c0bd2986e8844963a10f4e28ccada6f7b58857577c12cde4dd3ff6a273/Office365-REST-Python-Client-2.5.8.tar.gz",
    "platform": null,
    "description": "# About\nMicrosoft 365 & Microsoft Graph library for Python\n\n# Usage\n\n1. [Installation](#Installation)\n2. [Working with SharePoint API](#Working-with-SharePoint-API) \n3. [Working with Outlook API](#Working-with-Outlook-API) \n4. [Working with OneDrive and SharePoint API v2 APIs](#working-with-onedrive-and-sharepoint-v2-apis)\n5. [Working with Teams API](#Working-with-Microsoft-Teams-API)\n6. [Working with OneNote API](#Working-with-Microsoft-OneNote-API)\n7. [Working with Planner API](#Working-with-Microsoft-Planner-API)  \n\n## Status\n[![Downloads](https://pepy.tech/badge/office365-rest-python-client/month)](https://pepy.tech/project/office365-rest-python-client)\n[![PyPI](https://img.shields.io/pypi/v/Office365-REST-Python-Client.svg)](https://pypi.python.org/pypi/Office365-REST-Python-Client)\n[![PyPI pyversions](https://img.shields.io/pypi/pyversions/Office365-REST-Python-Client.svg)](https://pypi.python.org/pypi/Office365-REST-Python-Client/)\n[![Build Status](https://travis-ci.com/vgrem/Office365-REST-Python-Client.svg?branch=master)](https://travis-ci.com/vgrem/Office365-REST-Python-Client)\n\n# Installation\n\nUse pip:\n\n```\npip install Office365-REST-Python-Client\n```\n\n### Note \n>\n>Alternatively the _latest_ version could be directly installed via GitHub:\n>```\n>pip install git+https://github.com/vgrem/Office365-REST-Python-Client.git\n>```\n\n# Authentication\nFor the following examples, relevant credentials can be found in the Azure Portal.\n\nSteps to access:\n1. Login to the home page of the Azure Portal\n2. Navigate to \"Azure Active Directory\" using the three bars in the top right corner of the portal\n3. Select \"App registrations\" in the navigation panel on the left\n4. Search for and select your relevant application\n5. In the application's \"Overview\" page, the client id can be found under \"Application (client) id\"\n6. In the application's \"Certificates & Secrets\" page, the client secret can be found under the \"Value\" of the \"Client Secrets.\" If there is no client secret yet, create one here.\n\n\n# Working with SharePoint API\n\n   The `ClientContext` client provides the support for a legacy SharePoint REST and OneDrive for Business REST APIs, \n   the list of supported versions: \n   -   [SharePoint 2013 REST API](https://msdn.microsoft.com/en-us/library/office/jj860569.aspx) and above \n   -   [SharePoint Online REST API](https://learn.microsoft.com/en-us/sharepoint/dev/sp-add-ins/get-to-know-the-sharepoint-rest-service)\n   -   OneDrive for Business REST API\n\n### Authentication\n\n   The following auth flows are supported:\n\n#### 1. Using a SharePoint App-Only principal (client credentials flow)\n\n   This auth method is compatible with SharePoint on-premises and still relevant \n   model in both SharePoint on-premises as SharePoint Online, \n   the following methods are available: \n\n   - `ClientContext.with_credentials(client_credentials)` \n   - `ClientContext.with_client_credentials(client_id, client_secret)`\n  \n   Usage:\n   ``` \n   client_credentials = ClientCredential('{client_id}','{client_secret}')\n   ctx = ClientContext('{url}').with_credentials(client_credentials)\n   ```\n  \n   Documentation:\n   - [Granting access using SharePoint App-Only](https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azureacs)  \n   - [wiki](https://github.com/vgrem/Office365-REST-Python-Client/wiki/How-to-connect-to-SharePoint-Online-and-and-SharePoint-2013-2016-2019-on-premises--with-app-principal)\n  \n   Example: [connect_with_app_principal.py](examples/sharepoint/auth_app_only.py)\n  \n#### 2. Using username and password \n\n   Usage:\n   ``` \n   user_credentials = UserCredential('{username}','{password}')\n   ctx = ClientContext('{url}').with_credentials(user_credentials)\n   ```\n  \n   Example: [connect_with_user_credential.py](examples/sharepoint/auth_user_credential.py)\n  \n#### 3. Using an Azure AD application (certificate credentials flow) \n\n  Documentation: \n   - [Granting access via Azure AD App-Only](https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azuread)  \n   - [wiki](https://github.com/vgrem/Office365-REST-Python-Client/wiki/How-to-connect-to-SharePoint-Online-with-certificate-credentials) \n  \n  Example: [connect_with_client_certificate.py](examples/sharepoint/auth_client_certificate.py)\n\n#### 4. Interactive\n\n   to login interactively i.e. via a local browser\n\n   Prerequisite: \n   \n   > In Azure Portal, configure the Redirect URI of your\n   \"Mobile and Desktop application\" as ``http://localhost``.\n\n  Example: [connect_interactive.py](examples/sharepoint/auth_interactive.py)\n\n  Usage:\n```python\nfrom office365.sharepoint.client_context import ClientContext\nctx = ClientContext(site_url).with_interactive(tenant_name_or_id, client_id)\nme = ctx.web.current_user.get().execute_query()\nprint(me.login_name)\n```\n\n### Examples\n \nThere are **two approaches** available to perform API queries:\n\n1. `ClientContext class` - where you target SharePoint resources such as `Web`, `ListItem` and etc (recommended)\n \n```python\nfrom office365.runtime.auth.user_credential import UserCredential\nfrom office365.sharepoint.client_context import ClientContext\nsite_url = \"https://{your-tenant-prefix}.sharepoint.com\"\nctx = ClientContext(site_url).with_credentials(UserCredential(\"{username}\", \"{password}\"))\nweb = ctx.web\nctx.load(web)\nctx.execute_query()\nprint(\"Web title: {0}\".format(web.properties['Title']))\n```\nor alternatively via method chaining (a.k.a Fluent Interface): \n\n```python\nfrom office365.runtime.auth.user_credential import UserCredential\nfrom office365.sharepoint.client_context import ClientContext\nsite_url = \"https://{your-tenant-prefix}.sharepoint.com\"\nctx = ClientContext(site_url).with_credentials(UserCredential(\"{username}\", \"{password}\"))\nweb = ctx.web.get().execute_query()\nprint(\"Web title: {0}\".format(web.properties['Title']))\n```\n\n\n2. `RequestOptions class` - where you construct REST queries (and no model is involved)\n\n   The example demonstrates how to read `Web` properties:\n   \n```python\nimport json\nfrom office365.runtime.auth.user_credential import UserCredential\nfrom office365.runtime.http.request_options import RequestOptions\nfrom office365.sharepoint.client_context import ClientContext\nsite_url = \"https://{your-tenant-prefix}.sharepoint.com\"\nctx = ClientContext(site_url).with_credentials(UserCredential(\"{username}\", \"{password}\"))\nrequest = RequestOptions(\"{0}/_api/web/\".format(site_url))\nresponse = ctx.pending_request().execute_request_direct(request)\njson = json.loads(response.content)\nweb_title = json['d']['Title']\nprint(\"Web title: {0}\".format(web_title))\n```\n\nThe list of examples:\n\n- Working with files\n  - [download a file](examples/sharepoint/files/download.py) \n  - [upload a file](examples/sharepoint/files/upload.py)\n\n- Working with lists and list items\n  -  [create a list item](examples/sharepoint/lists/data_generator.py)\n  -  [read a list item](examples/sharepoint/lists/read_paged.py)   \n  -  [update a list item](examples/sharepoint/listitems/update_batch.py)\n  -  [delete a list item](examples/sharepoint/listitems/delete.py) \n  \nRefer [examples section](examples/sharepoint) for another scenarios\n\n### Support for non-standard SharePoint Online Environments\n\n  Support for non-standard SharePoint Environments is currently being implemented. Currently supported:\n  - GCC High\n\n  To enable authentication to GCC High endpoints, add the `environment='GCCH'` parameter when calling the \n  `ClientContext class` with `.with_user_credentials`, `.with_client_credentials`, or `.with_credentials`\n\n   Example:\n   ```python\n   client_credentials = ClientCredential('{client_id}','{client_secret}')\n   ctx = ClientContext('{url}').with_credentials(client_credentials, environment='GCCH')\n   ```\n\n# Working with Outlook API\n\nThe list of supported APIs:\n-   [Outlook Contacts REST API](https://msdn.microsoft.com/en-us/office/office365/api/contacts-rest-operations)\n-   [Outlook Calendar REST API](https://msdn.microsoft.com/en-us/office/office365/api/calendar-rest-operations)\n-   [Outlook Mail REST API](https://msdn.microsoft.com/en-us/office/office365/api/mail-rest-operations)\n\n\nSince Outlook REST APIs are available in both Microsoft Graph and the Outlook API endpoint, \nthe following clients are available:\n\n- `GraphClient` which targets Outlook API `v2.0` version (*preferable* nowadays, refer [transition to Microsoft Graph-based Outlook REST API](https://docs.microsoft.com/en-us/outlook/rest/compare-graph-outlook) for a details)   \n~~- `OutlookClient` which targets Outlook API `v1.0` version (not recommended for usage since `v1.0` version is being deprecated.)~~\n\n\n### Authentication\n\n[The Microsoft Authentication Library (MSAL) for Python](https://pypi.org/project/msal/) which comes as a dependency \nis used as a default library to obtain tokens to call Microsoft Graph API. \n\nUsing [Microsoft Authentication Library (MSAL) for Python](https://pypi.org/project/msal/)\n\n> Note: access token is getting acquired via [Client Credential flow](https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-client-creds-grant-flow)\n> in the provided examples. Other forms of token acquisition can be found here: https://msal-python.readthedocs.io/en/latest/\n\n```python\nimport msal\nfrom office365.graph_client import GraphClient\n\ndef acquire_token():\n    \"\"\"\n    Acquire token via MSAL\n    \"\"\"\n    authority_url = 'https://login.microsoftonline.com/{tenant_id_or_name}'\n    app = msal.ConfidentialClientApplication(\n        authority=authority_url,\n        client_id='{client_id}',\n        client_credential='{client_secret}'\n    )\n    token = app.acquire_token_for_client(scopes=[\"https://graph.microsoft.com/.default\"])\n    return token\n\n\nclient = GraphClient(acquire_token)\n\n```\n\nBut in terms of Microsoft Graph API authentication, another OAuth spec compliant libraries \nsuch as [adal](https://github.com/AzureAD/azure-activedirectory-library-for-python) \nare supported as well.   \n\nUsing [ADAL Python](https://adal-python.readthedocs.io/en/latest/#)\n\nUsage\n\n```python\nimport adal\nfrom office365.graph_client import GraphClient\n\ndef acquire_token_func():\n    authority_url = 'https://login.microsoftonline.com/{tenant_id_or_name}'\n    auth_ctx = adal.AuthenticationContext(authority_url)\n    token = auth_ctx.acquire_token_with_client_credentials(\n        \"https://graph.microsoft.com\",\n        \"{client_id}\",\n        \"{client_secret}\")\n    return token\n\nclient = GraphClient(acquire_token_func)\n\n```\n\n#### Example\n\nThe example demonstrates how to send an email via [Microsoft Graph endpoint](https://docs.microsoft.com/en-us/graph/api/user-sendmail?view=graph-rest-1.0&tabs=http).\n\n> Note: access token is getting acquired  via [Client Credential flow](https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-client-creds-grant-flow)\n\n```python\nfrom office365.graph_client import GraphClient\n\nclient = GraphClient(acquire_token_func)\n\nclient.me.send_mail(\n    subject=\"Meet for lunch?\",\n    body=\"The new cafeteria is open.\",\n    to_recipients=[\"fannyd@contoso.onmicrosoft.com\"]\n).execute_query()\n\n```\n\n\nAdditional examples & scenarios:\n\n-  [download a message](examples/outlook/messages/download.py) \n-  [list messages](examples/outlook/messages/list_all.py)\n-  [move messages to a different folder](examples/outlook/messages/move.py)\n-  [search messages](examples/outlook/messages/search.py)   \n-  [send messages](examples/outlook/messages/send.py)\n-  [send messages with attachments](examples/outlook/messages/send_with_attachment.py) \n-  [enable sending emails on behalf of another user in your organization](https://learn.microsoft.com/en-us/microsoft-365/solutions/allow-members-to-send-as-or-send-on-behalf-of-group)\n\nRefer to [examples section](examples/outlook) for other scenarios\n\n\n# Working with OneDrive and SharePoint v2 APIs\n\n#### Documentation \n\n[OneDrive Graph API reference](https://docs.microsoft.com/en-us/graph/api/resources/onedrive?view=graph-rest-1.0)\n\n#### Authentication\n\n[The Microsoft Authentication Library (MSAL) for Python](https://pypi.org/project/msal/) which comes as a dependency \nis used to obtain token\n\n```python\nimport msal\n\ndef acquire_token_func():\n    \"\"\"\n    Acquire token via MSAL\n    \"\"\"\n    authority_url = 'https://login.microsoftonline.com/{tenant_id_or_name}'\n    app = msal.ConfidentialClientApplication(\n        authority=authority_url,\n        client_id='{client_id}',\n        client_credential='{client_secret}'\n    )\n    token = app.acquire_token_for_client(scopes=[\"https://graph.microsoft.com/.default\"])\n    return token\n``` \n\n\n#### Examples \n\n##### Example: list available drives\n\nThe example demonstrates how to enumerate and print drive's url \nwhich corresponds to [`list available drives` endpoint](https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/drive_list?view=odsp-graph-online)\n\n> Note: access token is getting acquired  via [Client Credential flow](https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-client-creds-grant-flow)\n\n```python\nfrom office365.graph_client import GraphClient\n\ntenant_name = \"contoso.onmicrosoft.com\"\nclient = GraphClient(acquire_token_func)\ndrives = client.drives.get().execute_query()\nfor drive in drives:\n    print(\"Drive url: {0}\".format(drive.web_url))\n```\n\n\n##### Example: download the contents of a DriveItem(folder facet)\n\n```python\nfrom office365.graph_client import GraphClient\nclient = GraphClient(acquire_token_func)\n# retrieve drive properties \ndrive = client.users[\"{user_id_or_principal_name}\"].drive.get().execute_query()\n# download files from OneDrive into local folder \nwith tempfile.TemporaryDirectory() as path:\n    download_files(drive.root, path)\n```\n\nwhere\n\n```python\ndef download_files(remote_folder, local_path):\n    drive_items = remote_folder.children.get().execute_query()\n    for drive_item in drive_items:\n        if drive_item.file is not None:  # is file?\n            # download file content\n            with open(os.path.join(local_path, drive_item.name), 'wb') as local_file:\n                drive_item.download(local_file).execute_query()\n```\n\nAdditional examples:\n\n-  [create list column](examples/onedrive/columns/create_text.py) \n-  [download file](examples/onedrive/files/download.py)\n-  [export files](examples/onedrive/files/export.py)\n-  [upload folder](examples/onedrive/folders/upload.py)   \n-  [list drives](examples/onedrive/drives/list.py)\n-  [list files](examples/onedrive/folders/list_files.py)\n\nRefer to [OneDrive examples section](examples/onedrive) for more examples.\n\n\n# Working with Microsoft Teams API\n\n#### Authentication\n\n[The Microsoft Authentication Library (MSAL) for Python](https://pypi.org/project/msal/) which comes as a dependency \nis used to obtain token  \n\n#### Examples \n\n##### Example: create a new team under a group\n\nThe example demonstrates how create a new team under a group \nwhich corresponds to [`Create team` endpoint](https://docs.microsoft.com/en-us/graph/api/team-put-teams?view=graph-rest-1.0&tabs=http)\n\n```python\nfrom office365.graph_client import GraphClient\nclient = GraphClient(acquire_token_func)\nnew_team = client.groups[\"{group_id}\"].add_team().execute_query_retry()\n```\n\nAdditional examples:\n\n-  [create a team](examples/teams/create_team.py) \n-  [create team from group](examples/teams/create_from_group.py)\n-  [list all teams](examples/teams/list_all.py)\n-  [list my teams](examples/teams/list_my_teams.py)   \n-  [send messages](examples/teams/send_message.py)\n  \nRefer to [examples section](examples/teams) for other scenarios\n\n# Working with Microsoft Onenote API\n\nThe library supports OneNote API in terms of calls to a user's OneNote notebooks, sections, and pages in a personal or organization account\n\nExample: Create a new page\n\n```python\nfrom office365.graph_client import GraphClient\nclient = GraphClient(acquire_token_func)\n\nfiles = {}\nwith open(\"./MyPage.html\", 'rb') as f, \\\n    open(\"./MyImage.png\", 'rb') as img_f, \\\n    open(\"./MyDoc.pdf\", 'rb') as pdf_f:\n    files[\"imageBlock1\"] = img_f\n    files[\"fileBlock1\"] = pdf_f\n    page = client.me.onenote.pages.add(presentation_file=f, attachment_files=files).execute_query()\n```\n\n# Working with Microsoft Planner API\n\nThe example demonstrates how to create a new planner task\nwhich corresponds to [`Create plannerTask` endpoint](https://docs.microsoft.com/en-us/graph/api/planner-post-tasks?view=graph-rest-1.0):\n\n```python\nfrom office365.graph_client import GraphClient\n\nclient = GraphClient(acquire_token_func)\ntask = client.planner.tasks.add(title=\"New task\", planId=\"--plan id goes here--\").execute_query()\n```\n\n# Third Party Libraries and Dependencies\nThe following libraries will be installed when you install the client library:\n* [requests](https://github.com/kennethreitz/requests)\n* [Microsoft Authentication Library (MSAL) for Python](https://pypi.org/project/msal/)\n\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Microsoft 365 & Microsoft Graph Library for Python",
    "version": "2.5.8",
    "project_urls": {
        "Homepage": "https://github.com/vgrem/Office365-REST-Python-Client"
    },
    "split_keywords": [
        "git"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ccf638b128b443947538b78d79a28e98ef74c915df8a1ea5e255b5e73c0563cc",
                "md5": "5a5586868842639873f5577bc7d45c6e",
                "sha256": "d886a9ed9f02d5030c8b2e7da8f405110aa4b2e710f4e526a1b8c8386bb20326"
            },
            "downloads": -1,
            "filename": "Office365_REST_Python_Client-2.5.8-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "5a5586868842639873f5577bc7d45c6e",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 1159568,
            "upload_time": "2024-04-14T16:11:07",
            "upload_time_iso_8601": "2024-04-14T16:11:07.998102Z",
            "url": "https://files.pythonhosted.org/packages/cc/f6/38b128b443947538b78d79a28e98ef74c915df8a1ea5e255b5e73c0563cc/Office365_REST_Python_Client-2.5.8-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a12c71c0bd2986e8844963a10f4e28ccada6f7b58857577c12cde4dd3ff6a273",
                "md5": "d4c3fbeeb0eeba2583c56e43e7457027",
                "sha256": "66b1e4a488f1f783e1666905774a207050c12aa63c2a2676aa4d636ca183f188"
            },
            "downloads": -1,
            "filename": "Office365-REST-Python-Client-2.5.8.tar.gz",
            "has_sig": false,
            "md5_digest": "d4c3fbeeb0eeba2583c56e43e7457027",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 600883,
            "upload_time": "2024-04-14T16:11:11",
            "upload_time_iso_8601": "2024-04-14T16:11:11.249089Z",
            "url": "https://files.pythonhosted.org/packages/a1/2c/71c0bd2986e8844963a10f4e28ccada6f7b58857577c12cde4dd3ff6a273/Office365-REST-Python-Client-2.5.8.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-14 16:11:11",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "vgrem",
    "github_project": "Office365-REST-Python-Client",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "requests",
            "specs": [
                [
                    "==",
                    "2.31.0"
                ]
            ]
        },
        {
            "name": "requests_ntlm",
            "specs": []
        },
        {
            "name": "setuptools",
            "specs": [
                [
                    "==",
                    "65.5.1"
                ]
            ]
        },
        {
            "name": "msal",
            "specs": [
                [
                    "==",
                    "1.24.1"
                ]
            ]
        },
        {
            "name": "pytz",
            "specs": [
                [
                    "==",
                    "2021.1"
                ]
            ]
        },
        {
            "name": "typing_extensions",
            "specs": [
                [
                    ">=",
                    "4.0.0"
                ]
            ]
        }
    ],
    "lcname": "office365-rest-python-client"
}
        
Elapsed time: 0.31676s