-1

I want to list and download files from public Sharepoint directory. I have link in format https://xxx.sharepoint.com/:f:/s/site_name/long_id?e=shorter_id
which redirects to
https://xxx.sharepoint.com/sites/site_name/Shared%20Documents/Forms/AllItems.aspx?cid=some_id&RootFolder=encoded_path&FolderCTID=another_is

For not public files I'm using Office365-REST-Python-Client and real account credentials but this library doesn't allow for usage without auth.

As it seems for relative easy task I could use bare REST API but as I'm complete Sharepoint newbie I could use some tip where to start looking.

Banciur
  • 75
  • 6
  • What have you tried so far? – Underoos May 13 '19 at 12:31
  • Python library I mentioned above but it's not working without auth. I tried sending bare request showed in official doc [Working with folders and files with REST](https://learn.microsoft.com/en-us/sharepoint/dev/sp-add-ins/working-with-folders-and-files-with-rest) but it also is not working without auth headers. – Banciur May 13 '19 at 12:34
  • Can you please share your code. – Underoos May 13 '19 at 12:43
  • Library I mentioned earlier doesn't have option to work without auth creds. Just out of curiosity I tried passing empty strings instead login / pass but as suspected it didn't work. Bare request is like this `http "https://xxx.sharepoint.com/sites/site_name/_api/web/GetFolderByServerRelativeUrl('path of directory')"` (I'm using http-pie) and get 403 error with error message suggesting that I need to authorize request. – Banciur May 13 '19 at 12:55
  • duplicate of https://stackoverflow.com/questions/65286911/how-to-retrieve-json-file-via-rest-api-and-download-it-in-sharepoint/74522243#74522243 – john k Nov 21 '22 at 16:48
  • @johnktejik did you bother checking dates of questions? :) – Banciur Nov 23 '22 at 05:48
  • @Banciur I did actually. Why? – john k Nov 23 '22 at 14:56

1 Answers1

1

I think it might be not possible to archive using Sharepoint API's (at least couldn't find how) but it's doable by processing bare requests.
It seems under the hood Sharepoint performs some automatic authorization and perform several redirects attaching authorization cookies.
You just have to use some real User-Agent for all requests.

Steps are following:

  • GET https://xxx.sharepoint.com/:f:/s/site_name/long_id?e=shorter_id you will receive 301 to authorization endpoint
  • GET https://xxx.sharepoint.com/sites/site_name/_layouts/15/guestaccess.aspx?e=shorter_id&share=long_id. This one will setup some cookies and 302 to your destination
  • GET https://xxx.sharepoint.com/sites/site_name/Shared%20Documents/path_to_dir?cid=some_id this one will return real data. You must use auth cookies from previous step.
  • In response look for script tag with javascript ListData object. It will contain structure describing all files. You are interested in FileRef attribute.
  • GET https://xxx.sharepoint.com/sites/site_name/_layouts/15/download.aspx?SourceUrl=FileRef it will download your data. Remember about auth header.

It's less then optimal but works for me.

Banciur
  • 75
  • 6