How can i get access to Sharepoint documents using Python

kamal2222ahmed 1 Reputation point
2021-08-18T16:14:08.337+00:00

i tried using https://github.com/JonathanHolvey/sharepy
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 = "https://my.sharepoint.com"
 username = 'abcd123'
 password =  '1234' 
site = "https://my.sharepoint.com/:x:/r/personal/601600/_layouts/15/" 
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()
else:
# 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:

python sharepy_bah.py
Authentication successful
Traceback (most recent call last):
File "sharepy_bah.py", 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/session.py", line 157, in getfile
filename = kwargs.pop("filename", re.search(r"[^/]+$", url).group(0))
AttributeError: 'NoneType' object has no attribute 'group'

Microsoft 365 and Office SharePoint Server For business
Microsoft 365 and Office SharePoint Development
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. MichaelHan-MSFT 18,126 Reputation points
    2021-08-19T01:59:00.57+00:00

    Hi @kamal2222ahmed ,

    You could use Office365-Rest-Python-Client library to access to Sharepoint documents. https://github.com/vgrem/Office365-REST-Python-Client

    Here are some examples about file operations: https://github.com/vgrem/Office365-REST-Python-Client/tree/master/examples/sharepoint/files


    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.