3

The goal of this code is to use Python to read an Excel file from a SharePoint folder using the Office365-REST-Python-Client 2.1.7.post1 module. I have worked with our IT Security Team to get ClientID and Client Secret codes for the authorization token and have verified they are working. But, whenever I attempt to access a given Excel file in a document library folder, I'm getting TypeError: 'NoneType' object is not subscriptable errors.

Here's the code:

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

credentials = {
    'credurl': 'https://portal.[COMPANYNAME].com/sites/documentlibrary',
    'client_id': '[CLIENT ID CODE]',
    'client_secret': '[CLIENT SECRET]',
}
relative_url = '/sites/documentlibrary/testfolder/Test-2020-05-01.xlsx'

context_auth = AuthenticationContext(url=credentials['credurl'])
context_auth.acquire_token_for_app(client_id=credentials['client_id'], client_secret=credentials['client_secret'])
ctx = ClientContext(credentials['credurl'], context_auth)

filename = 'output-2020-05-02.xlsx'
with open(filename, 'wb') as output_file:
    response = File.open_binary(ctx, relative_url)
    output_file.write(response.content)

The code errors our at the line response = File.open_binary(ctx, relative_url). Here's the full error:

Traceback (most recent call last):
  File "[NETWORK PATH]/Post-Test.py", line 46, in <module>
    response = File.open_binary(ctx=ctx, server_relative_url=relative_url)
  File "C:\Program Files\Python38\lib\site-packages\office365\sharepoint\file.py", line 201, in open_binary
    response = ctx.execute_request_direct(request)
  File "C:\Program Files\Python38\lib\site-packages\office365\runtime\client_runtime_context.py", line 34, in execute_request_direct
    return self.pending_request.execute_request_direct(request)
  File "C:\Program Files\Python38\lib\site-packages\office365\runtime\client_request.py", line 41, in execute_request_direct
    self.context.authenticate_request(request_options)
  File "C:\Program Files\Python38\lib\site-packages\office365\runtime\client_runtime_context.py", line 15, in authenticate_request
    self.__auth_context.authenticate_request(request)
  File "C:\Program Files\Python38\lib\site-packages\office365\runtime\auth\authentication_context.py", line 37, in authenticate_request
    request_options.set_header('Authorization', self.provider.get_authorization_header())
  File "C:\Program Files\Python38\lib\site-packages\office365\runtime\auth\acs_token_provider.py", line 76, in get_authorization_header
    return 'Bearer {0}'.format(self.access_token["access_token"])
TypeError: 'NoneType' object is not subscriptable

Any help is appreciated!

3
  • 1
    I think the content on this page can help you! Check out: stackoverflow.com/questions/48424045/… Commented May 6, 2020 at 17:14
  • I think this content can help! Check out this: enter link description here Commented May 6, 2020 at 17:21
  • That's where I started. Even so, I can't get that solution to work. But thank you for responding. Commented May 6, 2020 at 17:21

1 Answer 1

3

Here is a sample that demonstrates how to download file from sharepoint using Office365-REST-Python-Client.

File.open_binary

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.