question

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

office-sharepoint-server-customizationsharepoint-dev
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. 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.

· 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 https://github.com/vgrem/Office365-REST-Python-Client/blob/master/examples/sharepoint/files/download_file.py

from tests import test_team_site_url, test_client_credentials

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

The closest named file is test_team_site.py
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 ·

@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}.sharepoint.com/sites/test"
 ctx = ClientContext(site_url).with_credentials(UserCredential("{username}", "{password}"))
0 Votes 0 ·

@kamal2222ahmed-0230,
Is there anything update? Have you got it resolved?

0 Votes 0 ·

I had the same question arising from inquiry into the upload_file.py. I looked around and see references to test_user_credentials and test_team_site_url located here:
https://github.com/vgrem/Office365-REST-Python-Client/blob/master/tests/init.py
I have yet to test the init.py out though

0 Votes 0 ·