Giter Club home page Giter Club logo

office365-rest-python-client's Introduction

About

Microsoft 365 & Microsoft Graph library for Python

Usage

  1. Installation
  2. Working with SharePoint API
  3. Working with Outlook API
  4. Working with OneDrive and SharePoint API v2 APIs
  5. Working with Teams API
  6. Working with OneNote API
  7. Working with Planner API

Status

Downloads PyPI PyPI pyversions Build Status

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:

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:

Example: connect_with_app_principal.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

3. Using an Azure AD application (certificate credentials flow)

Documentation:

Example: connect_with_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

Usage:

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)
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):

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']))
  1. SharePointRequest class - where you construct REST queries (and no model is involved)

    The example demonstrates how to read Web properties:

import json
from office365.runtime.auth.user_credential import UserCredential
from office365.sharepoint.request import SharePointRequest
site_url = "https://{your-tenant-prefix}.sharepoint.com"
request = SharePointRequest(site_url).with_credentials(UserCredential("{username}", "{password}"))
response = request.execute_request("web")
json = json.loads(response.content)
web_title = json['d']['Title']
print("Web title: {0}".format(web_title))

The list of examples:

Refer examples section 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:

from office365.sharepoint.client_context import ClientContext
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:

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 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 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

Note: access token is getting acquired via Client Credential flow in the provided examples. Other forms of token acquisition can be found here: https://msal-python.readthedocs.io/en/latest/

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 are supported as well.

Using ADAL Python

Usage

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.

Note: access token is getting acquired via Client Credential flow

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=["[email protected]"]
).execute_query()

Additional examples & scenarios:

Refer to examples section for other scenarios

Working with OneDrive and SharePoint v2 APIs

Documentation

OneDrive Graph API reference

Authentication

The Microsoft Authentication Library (MSAL) for Python which comes as a dependency is used to obtain token

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

Note: access token is getting acquired via Client Credential flow

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)
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

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:

Refer to OneDrive examples section for more examples.

Working with Microsoft Teams API

Authentication

The Microsoft Authentication Library (MSAL) for Python 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

from office365.graph_client import GraphClient
client = GraphClient(acquire_token_func)
new_team = client.groups["{group-id}"].add_team().execute_query_retry()

Additional examples:

Refer to examples section 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

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:

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:

ThanksTo

Powerful Python IDE Pycharm from Jetbrains.

office365-rest-python-client's People

Contributors

aisbergg avatar andreas-j-hauser avatar beliaev-maksim avatar caleb-depotanalytics avatar deadscop avatar dependabot[bot] avatar domdinicola avatar gd-kgadek avatar hsluoyz avatar jojo0severo avatar juguerre avatar kellerza avatar kfsz avatar liuliqiu avatar lsgd avatar nsmcan avatar padamscrestonecapital avatar philipp-c avatar rebeccajhampton avatar rikeshtailor avatar takatsugu-kato avatar thaiphv avatar theodoriss avatar timurgen avatar toilal avatar tomkcook avatar vgrem avatar wreiner avatar yusuf-asm avatar zahlii avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

office365-rest-python-client's Issues

office365.runtime.auth.saml_token_provider.SamlTokenProvider.acquire_authentication_cookie

I essentially cut and pasted examples into ipython and received the following error:

No handlers could be found for logger "office365.runtime.auth.saml_token_provider.SamlTokenProvider.acquire_authentication_cookie"
Traceback (most recent call last):
  File "outlook365-calendar-read.py", line 19, in <module>
    data = request.execute_request_direct(options)
AttributeError: 'ClientRequest' object has no attribute 'execute_request_direct'

I could look into writing such a handler, but I'm at such an early stage with office365 api dev that I'm not even sure if that's a wild goose chase.

Fix examples

The examples use older API. After name convention changed, those examples do not work anymore.

Task

Fix them

Deleting list items does not work

The examples seem outdated when it comes to using ClientRequest objects. The constructor does not seem to take both a url and AuthenticationContext anymore and execute_query_direct now takes one RequestOptions object instead of several parameters.

I have tried to recreate the example using the current API, but have not succeeded. The below code at least, results in a requests library error: 'AuthenticationContext' object is not callable.

ctx_auth = AuthenticationContext(url)
        if ctx_auth.acquire_token_for_user(username, password):
            request = ClientRequest(ctx_auth)
            url = "{0}/_api/web/lists(guid'{1}')/items({2})".format(
                url,
                list_guid,
                item_id)
            options = RequestOptions(url)
            options.set_header('IF-MATCH', '*')
            options.set_header('X-HTTP-Method', 'DELETE')
            options.method = HttpMethod.Delete
            options.auth = ctx_auth
            data = request.execute_query_direct(options)
        else:
            print ctx_auth.get_last_error()

Can you please help me understand how to delete list items using the current API?

resource_path is not a str? or is a str type?

I'm trying to get the view list of views by using the example, but always met such issue.
could you help?

========== code
url="https://xxx.sharepoint.com/sites/xxxx"
ctx_auth = AuthenticationContext(url)
if ctx_auth.acquire_token_for_user(username, password):

ctx = ClientContext(url, ctx_auth)

web = ctx.web
#print("web resource_path{}".format(web.resource_path))

ctx.load(web)
ctx.execute_query()
print ("Web title: {0}".format(web.properties['Title']))

issue here when calling -> print_list_views(ctx)

======================

File "C:\python36\lib\site-packages\office365\sharepoint\view_collection.py", line 10, in get_by_title
resourcePath = self.resource_path + "/getbytitle('{0}')".format(view_title)
TypeError: unsupported operand type(s) for +: 'ResourcePathEntry' and 'str'

Post Headers

Hi,
I had to change the following two POST calls in saml_token_provider.py to resolve an authentication problem, can you integrate these changes?

  1. def acquire_service_token
  • response = requests.post(sts_url, data=request_body, headers={'Content-Type': 'application/x-www-form-urlencoded'})
  1. def acquire_authentication_cookie
  • session.post(url, data=self.token, headers={'Content-Type': 'application/x-www-form-urlencoded'})

Thanks
Andrea

Pip install is not referencing the latest version

Thanks for the great library.
A quick note for others, the pip install does not reference the latest changes.

For instance 'acquire_token_for_app' in the AuthenticationContext does not exist.

Cheers!

Unable to import

I am unable to import the module for some reason, i am using Python 3.6
Any ideas ?

Traceback (most recent call last):
File "C:/Users/me/Projects/o365/snippets/sharepoint.py", line 14, in
from office365.runtime.auth.authentication_context import AuthenticationContext
ModuleNotFoundError: No module named 'office365'

SSL Connection Error

i am facing the below error while trying through rest api

Error: HTTPSConnectionPool(host='xxxx', port=443)
retries exceeded with url: /_forms/default.aspx?wa=wsignin1.0 (Caused by S
r(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:777)'),))

Fix tests 1/2: failing test_1_upload_file

The problem is just with the paths:

$ pwd
SOMEPATH/Office365-REST-Python-Client
$ nosetests tests.test_file:TestFile.test_1_upload_file
E
======================================================================
ERROR: test_1_upload_file (tests.test_file.TestFile)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "SOMEPATH/Office365-REST-Python-Client/tests/test_file.py", line 25, in test_1_upload_file
    with open(self.source_path, 'r') as content_file:
IOError: [Errno 2] No such file or directory: '../examples/data/report.csv'
-------------------- >> begin captured logging << --------------------
requests.packages.urllib3.connectionpool: DEBUG: Starting new HTTPS connection (1): login.microsoftonline.com
requests.packages.urllib3.connectionpool: DEBUG: https://login.microsoftonline.com:443 "POST /extSTS.srf HTTP/1.1" 200 3824
requests.packages.urllib3.connectionpool: DEBUG: Starting new HTTPS connection (1): mediadev19.sharepoint.com
requests.packages.urllib3.connectionpool: DEBUG: https://mediadev19.sharepoint.com:443 "POST /_forms/default.aspx?wa=wsignin1.0 HTTP/1.1" 302 118
requests.packages.urllib3.connectionpool: DEBUG: https://mediadev19.sharepoint.com:443 "GET / HTTP/1.1" 302 179
requests.packages.urllib3.connectionpool: DEBUG: https://mediadev19.sharepoint.com:443 "GET /SitePages/DevHome.aspx HTTP/1.1" 200 47382
requests.packages.urllib3.connectionpool: DEBUG: Starting new HTTPS connection (1): mediadev19.sharepoint.com
requests.packages.urllib3.connectionpool: DEBUG: https://mediadev19.sharepoint.com:443 "GET /sites/contoso/_api/Web/lists/GetByTitle('Documents') HTTP/1.1" 200 None
--------------------- >> end captured logging << ---------------------

----------------------------------------------------------------------
Ran 1 test in 6.170s

FAILED (errors=1)
$ cd tests
$ nosetests tests.test_file:TestFile.test_1_upload_file
.
----------------------------------------------------------------------
Ran 1 test in 6.891s

OK

Unit tests fail

Even for older commits which originally succeeded, the nosetests fail. Appears that the server (the global state) changed:

$ git rev-parse --short @
7dc8de9

$ nosetests
...........E.........
======================================================================
ERROR: test_create_list_item (tests.test_listItem.TestListItem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/kgadek/PROJECT/Office365-REST-Python-Client/tests/test_listItem.py", line 14, in test_create_list_item
    self.context.execute_query()
  File "/Users/kgadek/PROJECT/Office365-REST-Python-Client/client/office365/runtime/client_runtime_context.py", line 33, in execute_query
    self.pending_request.execute_query()
  File "/Users/kgadek/PROJECT/Office365-REST-Python-Client/client/office365/runtime/client_request.py", line 23, in execute_query
    self.process_payload_json(qry, payload)
  File "/Users/kgadek/PROJECT/Office365-REST-Python-Client/client/office365/runtime/client_request.py", line 33, in process_payload_json
    raise ValueError("Response error:", payload['error']['message'])
ValueError: ('Response error:', {u'lang': u'en-US', u'value': u"List 'Tasks' does not exist at site with URL 'https://mediadev19.sharepoint.com/sites/contoso'."})
-------------------- >> begin captured logging << --------------------
requests.packages.urllib3.connectionpool: DEBUG: Starting new HTTPS connection (1): login.microsoftonline.com
requests.packages.urllib3.connectionpool: DEBUG: https://login.microsoftonline.com:443 "POST /extSTS.srf HTTP/1.1" 200 3824
requests.packages.urllib3.connectionpool: DEBUG: Starting new HTTPS connection (1): mediadev19.sharepoint.com
requests.packages.urllib3.connectionpool: DEBUG: https://mediadev19.sharepoint.com:443 "POST /_forms/default.aspx?wa=wsignin1.0 HTTP/1.1" 302 118
requests.packages.urllib3.connectionpool: DEBUG: https://mediadev19.sharepoint.com:443 "GET / HTTP/1.1" 302 179
requests.packages.urllib3.connectionpool: DEBUG: https://mediadev19.sharepoint.com:443 "GET /SitePages/DevHome.aspx HTTP/1.1" 200 46962
requests.packages.urllib3.connectionpool: DEBUG: Starting new HTTPS connection (1): mediadev19.sharepoint.com
requests.packages.urllib3.connectionpool: DEBUG: https://mediadev19.sharepoint.com:443 "POST /sites/contoso/_api/contextinfo HTTP/1.1" 200 None
requests.packages.urllib3.connectionpool: DEBUG: Starting new HTTPS connection (1): mediadev19.sharepoint.com
requests.packages.urllib3.connectionpool: DEBUG: https://mediadev19.sharepoint.com:443 "POST /sites/contoso/_api/Web/lists/GetByTitle('Tasks')/items HTTP/1.1" 404 None
--------------------- >> end captured logging << ---------------------

----------------------------------------------------------------------
Ran 21 tests in 101.906s

FAILED (errors=1)

Publish as PyPI package

Hi.

Did you consider publishing the code here as PyPI package? It works for my use case, which is exceptional! Being able to pip install office365-rest-client would be terrific.

I can help with this by providing PR with proper metadata. I also volunteer to maintain the package if you don't mind (the examples are bit outdated, but easy to fix).

Package and deploy new code

Since v1.1.0, there was only one but big change to codebase, namely #30.

$ git log --first-parent tags/v1.1.0..origin/master
commit a7516ea6112bcca2ed832501842c1a7d73149206 (HEAD -> master, origin/master, origin/HEAD)
Merge: d0641e7 cc96e6b
Author: Vadim Gremyachev <[email protected]>
Date:   Thu Jun 29 14:26:27 2017 +0300

    Merge pull request #30 from Maplecroft/master

    Move `office365` module to the top level

cc96e6b (merged above) changed the API of the package, so major version bump will be needed.

Listing files in a folder doesn't work as expected

I am working with a slightly modified version of file_operations.py example, as the Sharepoint instance I'm working with doesn't have any lists. Thus, I am trying to obtain files like so:

    folder = ctx.web.get_folder_by_server_relative_url('/')
    ctx.load(folder)
    ctx.execute_query()
    files = folder.files
    ctx.load(files)
    ctx.execute_query()

This yields ValueError: ('Response error:', {u'lang': u'en-US', u'value': u'Cannot find resource for the request Files.'})

Looking at the code, it seems that when the folders are fetched, the class constructor does not include the current resource path, and therefore when invoking folder.files, no valid URL is constructed. Indeed, the request becomes "GET //_api/Files" as opposed to the expected "GET http://myhost/_api/Web/GetFolderByServerRelativeUrl('%2f')/Files". Looks like a bug to me.

(Grepping for "__deferred" it doesn't seem that the resource URIs in __deferred objects are being parsed or used anywhere, so I imagine the URIs must be constructed from ResoucePath objects.)

Package doesn't contain SAML.xml file

File "/Users/kgadek/someproject/py2-venv/lib/python2.7/site-packages/client/office365/runtime/auth/saml_token_provider.py", line 112, in prepare_security_token_request
    f = open(os.path.join(os.path.dirname(__file__), 'SAML.xml'))
IOError: [Errno 2] No such file or directory: '/Users/kgadek/someproject/py2-venv/lib/python2.7/site-packages/client/office365/runtime/auth/SAML.xml'

Using app password

I love this client and have been using it very successfully for a while now. However, my organization has just enabled multi-factor authentication for Office 365 which has broken my application - ๐Ÿ‘Ž.

Do you have any examples you can share of using app passwords or other techniques for accessing O365 when multi-factor authentication is enabled?

Thanks!

Trouble uploading document with encoding

Attempting in various ways to upload a file to Sharepoint without dismissing the encoding. Just a text or log file, but after uploading, I'm seeing new line characters (\n) and carriage returns (\r) all over the place.

I've tried via ClientRequest and ClientContext, and have been able to upload files both to a Document Library and to a List, but I'm sort of stumped at this point. Is there a header I can set to let SharePoint know of the encoding?

Session and proxy support

Could be great to support proxy connections for those behind a web proxy. Please take into account NTLM proxies too.

... and support session instead of one request per operation. I.e make lots of request reusing same tcp/ssl connection.

Could be great to publish some big example code using class tests... Today the doc support is really poor and need to investigate inside the code to figure how to use it.

Thanks in advance for your great effort.

Release version 2.1.1, to PyPI?

Today I tried this library. I could get the v2.0.0 to work with Microsofts current Office365. I also tried checking out the GitHub version and installed it manually, this worked. Is it possible to release v2.1.1 to PyPI too so it can be installed with PIP?

NameError: name 'basestring' is not defined

I am getting an error when running your example code for file listing from Sharepoint.

NameError: name 'basestring' is not defined

@staticmethod
    def encode_method_value(value):
        if isinstance(value, basestring):
            value = "'{0}'".format(value)
        elif isinstance(value, bool):
            value = str(value).lower()
        return value

Lack Unicode support: UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)

Hi,

My Office 365 is in Chinese language. When I run the example: examples\user_group_operations.py, it gave me the error:

C:\Python27\python.exe J:/github_repos/Office365-REST-Python-Client/examples/user_group_operations.py
Traceback (most recent call last):
  File "J:/github_repos/Office365-REST-Python-Client/examples/user_group_operations.py", line 43, in <module>
    read_groups(ctx)
  File "J:/github_repos/Office365-REST-Python-Client/examples/user_group_operations.py", line 13, in read_groups
    print("Group title: {0}".format(group.properties["Title"]))
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)

Process finished with exit code 1

When I debug the code, I found that the returned groups contain Chinese characters, which causes the error.

def read_groups(ctx):
    """Read site groups example"""
    groups = ctx.web.site_groups
    ctx.load(groups)
    ctx.execute_query()

    for group in groups:
        print("Group title: {0}".format(group.properties["Title"]))  # <---- here contains non-English chars

Can you add the Unicode support for this library? Thanks.

How can i upload a file to sharepoint ?

I am trying to upload a file to sharepoint, am I on the right path ?

def upload_file(ctx, listTitle, path):
    list_obj = ctx.web.lists.get_by_title(listTitle)
    folder = list_obj.root_folder
    ctx.load(folder)
    ctx.execute_query()

    files = folder.files
    with open(path, 'rb') as f:
        content = f.read()
        file_creation_information = FileCreationInformation()
        file_creation_information.overwrite = True
        file_creation_information.url = path
        file_creation_information.content = content
        file_new = files.add(file_creation_information)

Getting more than 100 items from a list

I've been using examples/listitem_operations.py read_list_items() as a basis for a project I'm working on, and have hit a problem in that only the first 100 items in my list are returned.

This suggests that the underlying issue is that the underlying Sharepoint REST API is limited to returning 100 items at a time by default as a protection measure. There are some nasty hacks with $top= to increase the limit, but it appears the right way of retrieving an arbitrary number of items is to look for the existence of a link of the following form:

<feed>
   ...
   <link rel="next" href=".../items?%24skiptoken=Paged%3dTRUE%26p_ID%3d100" />
</feed> 

on each set of results (the ... above is a contraction of the REST resource URI). This indicates that there are additional results, and provides a link to the next page.

Is accessing paged results possible with this client? Apologies if I'm missing something, but I can't find anything immediately obvious.

How to update a single list item?

I am using the update method of SharePoint listitem.py
item_object.update()
but how can I put the data I want to update? for instance:
item_properties = {'__metadata': {'type': 'SP.Data.'+listTitle+'ListItem'},
'Title': 'new item',
'Value':99,
}
and then update the item...

An error occured while retrieving token: The entered and stored passwords do not match.

Hi @vgrem,

First of all, thanks a lot for this library!
I was trying this out to connect to SharePoint Online with Python but I directly got an error.
The code:

from client.auth.AuthenticationContext import AuthenticationContext
from client.ClientContext import ClientContext
from settings import settings

url = "https://mycompany.sharepoint.com/"
username = "[email protected]"
password = "password"


ctxAuth = AuthenticationContext(url)
if ctxAuth.acquireTokenForUser(username, password):
  request = ClientRequest(url,ctxAuth)
  requestUrl = "/_api/web/"   #Web resource endpoint
  data = request.executeQuery(requestUrl=requestUrl)

  webTitle = data['d']['Title']
  print "Web title: {0}".format(webTitle)

else:
  print ctxAuth.getLastErrorMessage()

I've added this to a Python file named connector.py. When executing this code I will get the error:

An error occured while retrieving token: The entered and stored passwords do not match.

So, why am I getting this and how should I fix this? On a sidenote: what is the correct way to install this library in Linux? I've now manually downloaded it and added it in the same folder where my connector.py file is but I know thats not really the good way.

Thanks,
Yenthe

How to add modules

It would be great with a simple installation guide! I appreciate your work!

Logging

I want to discuss adding logging to the system.

Use-case

The script I have failed once. Stacktrace points at saml_token_provider.py suggesting that the cookie from sharepoint was incorrect. I'd love to see more logs about this.

Proposal

  • use logging module throughout the library to trace variables and function calls,
  • function calls get logging.INFO level, while internal variables logging.debug unless they contain any secrets,
  • register new logging level logging.DEBUG_SECRETS=5 to log variables containing secrets. Very useful e.g. to inspect the cookies / SAML protocol.

Authentication Problems

Hi,

I use sharepoint in Cloud and from yesterday I have an authentication error: "An error occurred while retrieving token: AADSTS70002: Error validating credentials. AADSTS50126: Invalid username or password. An error occurred while retrieving auth cookies"

Do you have some similar issues? It's a problem of Azure AD??

many thanks for support

Andrea

ImportError: No module named client.AuthenticationContext

The sample in readme returns error.

$ ipython ./office365.py                                       
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
/usr/lib/python2.7/dist-packages/IPython/utils/py3compat.pyc in execfile(fname, *where)
    202             else:
    203                 filename = fname
--> 204             __builtin__.execfile(filename, *where)

/tmp/sharepoint_script/office365.py in <module>()
----> 1 from client.AuthenticationContext import AuthenticationContext
      2 from client.ClientRequest import ClientRequest
      3 
      4 url = "https://OURCORPORATE.sharepoint.com/"
      5 username = "[email protected]"

ImportError: No module named client.AuthenticationContext

More info

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 14.04.5 LTS
Release:        14.04
Codename:       trusty

$ pip list|grep -i Office365
Office365-REST-Python-Client (2.0.0)

$ ll office365.py 
-rwxr-xr-x 1 catcat 2cats 582 Nov  2 16:41 office365.py*

$ more office365.py 
from client.AuthenticationContext import AuthenticationContext
from client.ClientRequest import ClientRequest

url = "https://OURCORPORATE.sharepoint.com/"
username = "[email protected]"
password = "password_that_works"

ctxAuth = AuthenticationContext(url)
if ctxAuth.acquireTokenForUser(username, password):
  request = ClientRequest(url,ctxAuth)
  requestUrl = "/_api/web/"   #Web resource endpoint
  data = request.executeQuery(requestUrl=requestUrl)

  webTitle = data['d']['Title']
  print "Web title: {0}".format(webTitle)

else:
  print ctxAuth.getLastErrorMessage()

Delete all list items

Hi there!

I want to delete all items from a Sharepoint list.

list_object = ctx.web.lists.get_by_title(listname)
items = list_object.get_items()
ctx.load(items)
ctx.execute_query()
for item in items:
    item.delete_object()

This does not seem to work. Could anyone explain how to delete all items from a list?

Thanks in advance!

Python3

It is possible to add compatibility with Python3 ?

How to expose Page to html?

Hi,

Thanks for this great library.
I have a few questions

  • can we export Sharepoint page to html files?
  • Can we export List item to excel?
  • Can we download document?

My ultimate goal is exporting sharepoint and its associated contents to Google Sites but have no any clue for now.

Thanks,
Van

Fix tests 2/2: failing test_3_download_file

$ nosetests tests.test_file:TestFile.test_3_download_file
F
======================================================================
FAIL: Test file upload operation
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/kgadek/gd/git/m1e/Office365-REST-Python-Client/tests/test_file.py", line 41, in test_3_download_file
    self.assertEqual(response.content, '"' + self.report_content + '"')
AssertionError: '"123"' != '"Report data"'
-------------------- >> begin captured logging << --------------------
requests.packages.urllib3.connectionpool: DEBUG: Starting new HTTPS connection (1): login.microsoftonline.com
requests.packages.urllib3.connectionpool: DEBUG: https://login.microsoftonline.com:443 "POST /extSTS.srf HTTP/1.1" 200 3824
requests.packages.urllib3.connectionpool: DEBUG: Starting new HTTPS connection (1): mediadev19.sharepoint.com
requests.packages.urllib3.connectionpool: DEBUG: https://mediadev19.sharepoint.com:443 "POST /_forms/default.aspx?wa=wsignin1.0 HTTP/1.1" 302 118
requests.packages.urllib3.connectionpool: DEBUG: https://mediadev19.sharepoint.com:443 "GET / HTTP/1.1" 302 179
requests.packages.urllib3.connectionpool: DEBUG: https://mediadev19.sharepoint.com:443 "GET /SitePages/DevHome.aspx HTTP/1.1" 200 47348
requests.packages.urllib3.connectionpool: DEBUG: Starting new HTTPS connection (1): mediadev19.sharepoint.com
requests.packages.urllib3.connectionpool: DEBUG: https://mediadev19.sharepoint.com:443 "GET /sites/contoso/_api/Web/lists/GetByTitle('Documents') HTTP/1.1" 200 None
requests.packages.urllib3.connectionpool: DEBUG: Starting new HTTPS connection (1): mediadev19.sharepoint.com
requests.packages.urllib3.connectionpool: DEBUG: https://mediadev19.sharepoint.com:443 "GET /sites/contoso/_api/web/getfilebyserverrelativeurl('/sites/contoso/documents/report.csv')/%5C$value HTTP/1.1" 200 None
--------------------- >> end captured logging << ---------------------

----------------------------------------------------------------------
Ran 1 test in 3.633s

FAILED (failures=1)

oAuth not implemented yet

it looks like the repo has oAuth file in runtime/auth, but not implemented. (or at least it seems that way) Would there be any interest in the community for this feature?

Url lib package has to be changed for Python 3

instead of
from urlparse import urlparse

we need to use
from urllib.parse import urlparse

and finally under saml_token_provider.py
change urlparse.urlparse(self.url)
to
urlparse(self.url)

get_items() of a view

Is it possible to iterate through the items in a view like you can with a list ?

Thanks

Consider renaming (or dropping) the top-level package

The name client is pretty generic and seems unnecessary (since there's only one sub-module) - could you consider deleting it and having the top-level module be office365 instead? It would mean updating all the docs, tests, etc but that should be relatively easy with a simple find and replace...

Access denied when accessing folders

Hello!

I am an organisation collaborator and when I try to access to folders I get always Access denied. What am I missing here? Shouldn't I be able to access to my folders?

Sample code here:

def read_folder_and_files():

    print(ctx.web.service_root_url)
    """Read a folder example"""
    folders = ctx.web.folders
    ctx.load(folders)
    ctx.execute_query()
    for folder in folders:
        print("Folder name: {0}".format(folder.properties["Name"]))



if __name__ == '__main__':
    print(settings['url'])
    ctx_auth = AuthenticationContext(url=settings['url'])
    if ctx_auth.acquire_token_for_user(username=settings['user_credentials']['username'],
                                       password=settings['user_credentials']['password']):
        ctx = ClientContext(settings['url'], ctx_auth)

        read_folder_and_files()

    else:
        print(ctx_auth.get_last_error())

ValueError: ('Response error:', {u'lang': u'en-US', u'value': u'Access denied. You do not have permission to perform this action or access this resource.'})

Thank you in advance.

"TypeError: cannot concatenate 'str' and 'NoneType' objects" on request to get a file.

I'm trying to download a file which is accessible (via a browser) on this URL:
https://test.sharepoint.com/sites/team/team documents/subfolder/document.docx

I'm using a ClientRequest object with the following code:

url = "https://test.sharepoint.com/sites/team"
username="[email protected]"
password="pass"
ctxAuth = AuthenticationContext(url)
if ctxAuth.acquire_token_for_user(username, password):
    print 'authentication successful, proceeding...'
    request = ClientRequest(ctxAuth)
    requestUrl="{0}/_api/web/getfilebyserverrelativeurl('team documents/subfolder/document.docx')"
    options=RequestOptions(requestUrl.format(url))
    data = request.execute_query_direct(options)

On running this, I'm getting the following trace:

$ python test.py
No handlers could be found for logger "client.office365.runtime.auth.saml_token_provider.SamlTokenProvider.process_service_token_response"
authentication successful, proceeding...
Traceback (most recent call last):
  File "test.py", line 14, in <module>
    data = request.execute_query_direct(options)
  File "/usr/lib/python2.7/site-packages/client/office365/runtime/client_request.py", line 77, in execute_query_direct
    self.context.authenticate_request(request_options)
  File "/usr/lib/python2.7/site-packages/client/office365/runtime/auth/authentication_context.py", line 20, in authenticate_request
    request_options.set_header('Cookie', self.provider.get_authentication_cookie())
  File "/usr/lib/python2.7/site-packages/client/office365/runtime/auth/saml_token_provider.py", line 65, in get_authentication_cookie
    return 'FedAuth=' + self.FedAuth + '; rtFa=' + self.rtFa
TypeError: cannot concatenate 'str' and 'NoneType' objects

Is there something wrong on how I use the module?

There were a few import issues

Encountered a few issues with imports when running your example code.

list_collection.py
changed from list import List to from .list import List

site.py
changed from web import Web to from .web import Web

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.