kamal2222ahmed-0230 avatar image
0 Votes"
kamal2222ahmed-0230 asked ONealChristopher-9023 commented

How can i get access to Sharepoint documents using Python

i tried using
and i am able to get authenticated, but do not know how to proceed.

  1. How to get a list of files/documents shared after i login ( like a pwd , and ls in Linux )

  2. How to get the PATH for the files

  3. How to get the Auth mechanism used ( i used username and password ) , but from browser we use SSO ( Single Sign On )

here is the code:

 import sharepy
 from sharepy import connect
 from sharepy import SharePointSession
 import logging
  SPUrl = ""
  username = 'abcd123'
  password =  '1234' 
 site = "" 
 s = sharepy.connect(SPUrl,username,password)
 # Create header for the http request
 my_headers = {
 'accept' : 'application/json;odata=verbose',
 'content-type' : 'application/json;odata=verbose',
 'odata' : 'verbose',
 'X-RequestForceAuthentication' : 'true'
 if not hasattr(s, 'cookie'):
     print("authentication failed!"); quit()
 # This will return a Requests response object. See the requests documentation for details. s.get() returns Requests response object
     r = s.getfile(site,filename = 'DASHBOARD.xlsx')
     print (r.status_code)
 print (r.raw)
 print("Script Complete")

but i get error:

Authentication successful
Traceback (most recent call last):
File "", line 31, in <module>
r = s.getfile(site,filename = 'PROGRAM-508-DASHBOARD.xlsx')
File "/Users/syedahmed/opt/anaconda3/envs/pandas/lib/python3.8/site-packages/sharepy/", line 157, in getfile
filename = kwargs.pop("filename","[^/]+$", url).group(0))
AttributeError: 'NoneType' object has no attribute 'group'

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

MichaelHan-MSFT avatar image
0 Votes"
MichaelHan-MSFT answered ONealChristopher-9023 commented

Hi @kamal2222ahmed-0230,

You could use Office365-Rest-Python-Client library to access to Sharepoint documents.

Here are some examples about file operations:

If an Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @MichaelHan-MSFT I am trying to use the Office365-REST Library, but having trouble with configuring credentials.

If we look at the file

from tests import test_team_site_url, test_client_credentials

This implies that there should be files named and in the tests directory, but in actuality, there aren't any of these files in the tests directory:

The closest named file is
with the following imports:
from tests import test_site_url, test_user_credentials

for which there are no corresponding files.

now, settings.cfg has some information about dummy users and client credentials. But these I need to get by contacting the Sharepoint Admin's
using sharepy , i was able to login with JUST username and password, but don't know how to proceed to get files displayed and download.
with Office365-REST Library, i am at a loss regarding the credentials. Also, how do settings.cfg gets used within a python script is unclear.

0 Votes 0 ·
MichaelHan-MSFT avatar image MichaelHan-MSFT kamal2222ahmed-0230 ·

You could connect with sharepoint site like the below:

 from office365.runtime.auth.user_credential import UserCredential
 from office365.sharepoint.client_context import ClientContext
 site_url = "https://{your-tenant-prefix}"
 ctx = ClientContext(site_url).with_credentials(UserCredential("{username}", "{password}"))
0 Votes 0 ·

Is there anything update? Have you got it resolved?

0 Votes 0 ·

I had the same question arising from inquiry into the I looked around and see references to test_user_credentials and test_team_site_url located here:
I have yet to test the out though

0 Votes 0 ·