Office365-rest-python-client: Requests MissingSchema Error when downloading file

Created on 2 Oct 2020  ·  4Comments  ·  Source: vgrem/Office365-REST-Python-Client

Hi!

I'm having trouble downloading a file from SharePoint using the latest 2.2.1 version of Office365. When I try to download a file a "MissingSchema" Error from the requests module appears. I don't know what happened, if I go back to version 2.1.5 the code runs fine. Thank you very much and I appreciate the hard work in this module; super useful!

from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.file import File

def auth(username, password):
    ctx_auth = AuthenticationContext(url)
    if ctx_auth.acquire_token_for_user(username, password):
      ctx = ClientContext(url, ctx_auth)
      return ctx

ctx = auth(username, password)
response = File.open_binary(ctx, relative_url)

Error:
MissingSchema: Invalid URL "<bound method ClientContext.service_root_url of <office365.sharepoint.client_context.ClientContext object at 0x7fb4e44c3d90>>web/getfilebyserverrelativeurl('MYRELATIVEURL')/\\$value": No schema supplied. Perhaps you meant http://<bound method ClientContext.service_root_url of <office365.sharepoint.client_context.ClientContext object at 0x7fb4e44c3d90>>web/getfilebyserverrelativeurl('MYRELATIVEURL')/\$value?

Using MacOS,
Office365 2.2.1,
Python 3.8.3,
requests 2.2.4

bug question

Most helpful comment

There is a bug in 2.2.1 version you either need to use latest from git or fix it manually in your installation here https://github.com/vgrem/Office365-REST-Python-Client/blob/master/office365/sharepoint/files/file.py#L275

Note that in you version ctx.service_root_url is a property but should be a method ctx.service_root_url()

All 4 comments

There is a bug in 2.2.1 version you either need to use latest from git or fix it manually in your installation here https://github.com/vgrem/Office365-REST-Python-Client/blob/master/office365/sharepoint/files/file.py#L275

Note that in you version ctx.service_root_url is a property but should be a method ctx.service_root_url()

Thank you @efremovd and @etiennecelery for catching and pinpoint it, indeed it is a bug and is encountered in version 2.2.1 due to refactoring service_root_url property to method.

As was mentioned, the _latest_ version available from GitHub resolves this issue:

pip install git+https://github.com/vgrem/Office365-REST-Python-Client.git

Alternatively, the file could be downloaded as demonstrated below:

file_url = '{server-relative-path-to-download}'
with open(download_path, "wb") as local_file:
    file = ctx.web.get_file_by_server_relative_url(file_url).download(local_file).execute_query()

Thank you!! Great work

Closing this one since has been resolved and 2.2.2 version has been released.

Was this page helpful?
0 / 5 - 0 ratings