How to use wget to download files from Onedrive? (and batch files and entire folders, if possible)
Update on APRIL 2021: It looks like this solution NO LONGER WORKS WITH ONEDRIVE FOR BUSINESS.
There is one way that works for me (based on How to Make Direct Link of OneDrive Files)
Right-click on the file you are interested in download (from web interface), and choose Embed.
Press "Generate HTML code to embed this file".
Copy the part contained in the
""of src is your link. This will look like <https://onedrive.live.com/embed?cid=6EBB03E38A53ED3E& resid=6EBB03E38A53ED3E%21116&authkey=AC4lDqtLG8LqfiA>.
download. This will look like https://onedrive.live.com/download?cid=6EBB03E38A53ED3E&resid=6EBB03E38A53ED3E%21116&authkey=AC4lDqtLG8LqfiA.
Feed it to
wgetusing following syntax (the quotes are required):
wget --no-check-certificate "https://onedrive.live.com/download?cid=6EBB03E38A53ED3E&resid=6EBB03E38A53ED3E%21116&authkey=AC4lDqtLG8LqfiA"
Using Chrome (but Firefox will probably also work).
- Open DevTools
- Click the Download button.
- Download but cancel immediately
- Open the 'Network' tab in DevTools. Search for 'Zip?authKey=' in DevTools and open it (click). This is a POST request.
- Click 'View source' to the right of 'Form data' at the bottom.
- Construct the command as follows:
wget --post-data='<raw form data>' '<Download URL>'
wget --post-data='resIds=xxx&canary=yyy&authkey=zzz' 'https://cid--foobar.users.storage.live.com/downloadfiles/V1/Zip?authKey=zzz'
This even works on a different host (with a different IP address).
generate a sharing link and append "&download=1" to the end of it.
e.g. if the link is "https://some.host/:z:/x/personal/some_user/123456asdf?e=12345"
Also, we can use CurlWget I already tried on OneDrive and Google Drive
If you are looking to download an entire publicly shared folder (without auth!): OneDeath
It uses wget to do some API calls and will then recursively download files. Bonus: sha1 checks of finished files.
Sharepoint is now also supported, but it's a little trickier (it currently fetches more files from a drive than what is shown by the link). A major problem was a slightly different API structure and the fact that the session token expires after 1h, but it's solved too. They also appear to have some rate-limiting in place, so don't start too many parallel downloads.
The script can extract ready-to-use
wget commands using the
PS: If you are interested to know more about the web API, I will give a run down of the process.
This python SDK (with example) for OneDrive project works (for personal, business, and corporate accounts):
You can embed the file and Replace embed with a download. This will look like https://onedrive.live.com/download?cid=6EBB03E38A53ED3E&resid=6EBB03E38A53ED3E%21116&authkey=AC4lDqtLG8LqfiA.
Now use PHP curl to get this file content
$download_url = "https://onedrive.live.com/download?cid=6EBB03E38A53ED3E&resid=6EBB03E38A53ED3E%21116&authkey=AC4lDqtLG8LqfiA"; $file_name = "test.xlsx"; $ch = curl_init($download_url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20); $data = curl_exec($ch); $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); $error = curl_errno($ch); curl_close($ch); // A file with the same name may exist, that must be handled. $file_save_path = $file_name; file_put_contents($file_save_path, $data); echo 'File successfully retrieved and stored!';