Skip to main content

Office 365 Library for Python

Project description

About

Office 365 & Microsoft Graph Library for Python

Usage

  1. Installation
  2. Working with SharePoint REST API
  3. Working with Outlook REST API
  4. Working with OneDrive REST API

Status

Build Status PyPI

Installation

Use pip:

pip install Office365-REST-Python-Client

Working with SharePoint REST API

The list of supported API versions:

Authentication

The following auth flows are supported:

  • app principals auth (refer Granting access using SharePoint App-Only for a details): AuthenticationContext.ctx_auth.acquire_token_for_app(client_id, client_secret)
  • user credentials auth: AuthenticationContext.ctx_auth.acquire_token_for_user(username, password)

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)
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext

ctx_auth = AuthenticationContext(url)
if ctx_auth.acquire_token_for_user(username, password):
  ctx = ClientContext(url, ctx_auth)
  web = ctx.web
  ctx.load(web)
  ctx.execute_query()
  print "Web title: {0}".format(web.properties['Title'])

else:
  print ctx_auth.get_last_error()
  1. ClientRequest class - where you construct REST queries by specifying endpoint url, headers if required and payload (aka low level approach)

    The example demonstrates how to read Web properties:

import json

from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.runtime.client_request import ClientRequest
from office365.runtime.utilities.request_options import RequestOptions

ctx_auth = AuthenticationContext(url)
if ctx_auth.acquire_token_for_user(username, password):
  request = ClientRequest(ctx_auth)
  options = RequestOptions("{0}/_api/web/".format(url))
  options.set_header('Accept', 'application/json')
  options.set_header('Content-Type', 'application/json')
  data = request.execute_request_direct(options)
  s = json.loads(data.content)
  web_title = s['Title']
  print "Web title: " + web_title
else:
  print ctx_auth.get_last_error()

Working with Outlook REST API

The list of supported APIs:

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

  • GraphClient which targets Outlook v2.0 version (preferable nowadays, refer transition to Microsoft Graph-based Outlook REST API for a details)
  • OutlookClient which targets Outlook v1.0 version (not recommended for usage since v1.0 version is being deprecated.)

Authentication

ADAL Python library is utilized to authenticate users to Active Directory (AD) and obtain tokens

Example

The example demonstrates how to send an email via Microsoft Graph endpoint.

Note: access token is getting acquired via Client Credential flow

def get_token(auth_ctx):
    token = auth_ctx.acquire_token_with_client_credentials(
        "https://graph.microsoft.com",
        client_id,
        client_secret)
    return token


tenant_name = "contoso.onmicrosoft.com"
client = GraphClient(tenant_name, get_token)

message_payload = {
    "Message": {
        "Subject": "Meet for lunch?",
        "Body": {
            "ContentType": "Text",
            "Content": "The new cafeteria is open."
        },
        "ToRecipients": [
            {
                "EmailAddress": {
                    "Address": "jdoe@contoso.onmicrosoft.com"
                }
            }
        ]
    },
    "SaveToSentItems": "false"
}

login_name = "mdoe@contoso.onmicrosoft.com"
client.users[login_name].send_mail(message_payload)
client.execute_query()

Working with OneDrive API

Documentation

OneDrive Graph API reference

Authentication

ADAL Python library is utilized to authenticate users to Active Directory (AD) and obtain tokens

Example

The example demonstrates how to print drive's url via list available drives endpoint

Note: access token is getting acquired via Client Credential flow

def get_token(auth_ctx):
    """Acquire token via client credential flow (ADAL Python library is utilized)"""
    token = auth_ctx.acquire_token_with_client_credentials(
        "https://graph.microsoft.com",
        client_id,
        client_secret)
    return token


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

Python Version

Python 2.7 & 3.4–3.6 are supported.

Third Party Libraries and Dependencies

The following libraries will be installed when you install the client library:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Office365-REST-Python-Client-2.1.5.tar.gz (40.5 kB view details)

Uploaded Source

File details

Details for the file Office365-REST-Python-Client-2.1.5.tar.gz.

File metadata

  • Download URL: Office365-REST-Python-Client-2.1.5.tar.gz
  • Upload date:
  • Size: 40.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.6.7

File hashes

Hashes for Office365-REST-Python-Client-2.1.5.tar.gz
Algorithm Hash digest
SHA256 81942dfa9b59ec5b1d48994f418d18f2762674f0c02a11589583d392e0e21e4b
MD5 173359e94cdecad0b8aa0484d216fc94
BLAKE2b-256 b2cb703f72f1ae7c4719d6e813c0ec1a12020c4a93af018476955e433968fca4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page