5
votes

I have a publicly shared OneDrive folder with some various sub-folders and files. I.e. I have shared it via a link, so anyone with this link can access them.

Is there a way I can access these files from either client-side JavaScript or some server-side code via a REST API of some sort? Without having to use any sort of user-specific credentials?


What I've been trying

I've been looking at the Accessing Shared Content page, and it looks like what I want, but can't figure out how to use it.

I've taken something that looks like an id from the shared URL, which looks to be a long hexadecimal number (which seems to be an id pointing at me?), an !, and then a number (which I assume has to do with the shared resource).

I've then tried to stick it in this URL:

https://api.onedrive.com/v1.0/shares/<id>/root?expand=children

But I get back a 400 Bad Request, so something isn't quite right...

I'm thinking maybe some sort of authentication is missing, but since the shared files are public, I don't users to have to login with their credentials, and can't of course use my own in code.

I've tried to register an app, where I get an application id (guid) and can generate Passwords and Key-Pairs. Was hoping maybe I could use that, but don't see in the API how and where to actually use those...


Goal

The shared folder contains sheet music for a choir, that I'm responsible for keeping updated (and OneDrive syncing is super handy here).

Some members aren't very computer savvy, so I'd like to make seeing and downloading these files as easy as possible. The shared link with a "go here to this other strange site and find the files there"-text sort of works, but I would much rather like to list the files directly in a member-only area of our website. Basically just "here are the files, click on one to download it".

1
I don't see anything in the question about JSON; is there a reason that tag is there?Heretic Monkey
@MikeMcCaughan Mostly that the rest api seemed to return JSON. I put it at the end though, but for some reason StackOverflow have ordered the tags from least relevant to most relevant :PSvish
So, some people (like me) follow the JSON tag for questions regarding the format, how to parse, etc.. Since this question doesn't have anything to do with the format, how to parse, etc., it would be preferable not to include the tag. Tags are meant to categorize what the question is about, not just a list of the technologies tangentially related to it.Heretic Monkey

1 Answers

9
votes

Yes, you can use the REST API to access the contents of a folder.

The API is the one you mentioned, the shares API. However, it sounds like you are perhaps using the wrong ID.

The most straightforward way to do this is to follow the instructions to encode the actual sharing URL into a token. This way you create a base64 encoded version of the sharing link, append a "u!" to the front of that string, and then make the exact call you already mentioned. You'll get back a list of the files in the shared folder and you can go from there.

Here's an example of this: Here's a sharing link to a folder in OneDrive with some photos in it.

https://1drv.ms/f/s!AtuAM_NacwVahiFpuMGS_BiQCwWu

To convert this URL into the API, you first base64 encode the URL and append u!

u!aHR0cHM6Ly8xZHJ2Lm1zL2YvcyFBdHVBTV9OYWN3VmFoaUZwdU1HU19CaVFDd1d1

Now you can use this URL as the sharing token, and expand children and thumbnails:

https://api.onedrive.com/v1.0/shares/u!aHR0cHM6Ly8xZHJ2Lm1zL2YvcyFBdHVBTV9OYWN3VmFoaUZwdU1HU19CaVFDd1d1/root?expand=children

Clicking on this bottom link should give you the JSON response, which includes the shared folder and the children inside the folder.