Simeon
Simeon

Reputation: 43

Download OneDrive file from cURL since they've changed their URL's construction

here's the problem i have.Before OneDrive changed the url's it was easy to force a file to be downloaded from cURL, but now since they've changed their URL's construction i can't use the previous approach of replacing redir? with download?. That's how they used to be constructed:

https://onedrive.live.com/redir?resid=

To make it start downloading immediately we just had to replace the redir with download like this

https://onedrive.live.com/download?resid=

and that was fixing it at all, but now the url's are like this:

https://1drv.ms/u/s!AsdMSpdm_dmsadMSda

So, my question is how can i download the file from cURL, i cannot do that even with CURLOPT_FOLLOWLOCATION, it is only working if the file is forced to be downloaded. Thanks in advance!

Upvotes: 4

Views: 22577

Answers (2)

Ricardo Martins
Ricardo Martins

Reputation: 6003

I found 3 ways to achieve that..

1. Following the Redirect

This option doesn't always work, because sometimes the redirected url also contains !.

  1. Open your 1drv.ms link in the browser. It will redirect you to some big url (1drv.com url).
  2. Copy the big URL
  3. And use it with cURL Following the location. For example: curl https://wie6yw.dm.files.1drv.com/y4mRNH415nArXijS4qtGN-bTq9plsHtfUJNq2M -L -o OUTPUTFILE.EXT

Just remember to rename OUTPUTFILE.EXT in the end with the original filename.

2. Using external tool to convert it

This guy created an online tool to convert OneDrive links. It works, but if you do that in a daily basis this could be annoying.

  1. View the file you want to share on web
  2. Click to get the embedded code at OneDrive website
  3. Paste the code in this online tool 4.Click Get Download link.
  4. Use the generated download url in cURL.

3. Creating my own redirect in PHP

I used the @Ryan Gregg's approach above and created a converter.php to convert OneDrive URLs and redirect the user to the final file. This way I can use it with cURL.

  1. Create a php file (i.e.: converter.php) with the following:
<?php
$sharedUrl = urldecode($_GET['url']);
$base64 = base64_encode($sharedUrl);
$encodedUrl = "u!" . rtrim($base64, '=');
$encodedUrl = str_replace('/', '_', $encodedUrl);
$encodedUrl = str_replace('+', '-', $encodedUrl);

$final = sprintf('https://api.onedrive.com/v1.0/shares/%s/root/content', $encodedUrl);
header('Location:' . $final, true, 302);
  1. URL Encode the shared OneDrive URL. You can use an online tool if you want.

  2. Call your php file in curl and pass the encoded URL as parameter in ?url=. For example: curl http://localhost/converter.php?url=https%3A%2F%2F1drv.ms%2Fu%2Fs%21ApzSTOgc2rAs4d2olMS%3Fe%3DrZIUN -L -o OUTPUTFILE.EXT. Replace the OUTPUTFILE.EXT with the original file name.

Upvotes: 0

Ryan Gregg
Ryan Gregg

Reputation: 2035

There are two options. The "right" way to do this would be to encode the full sharing URL following the details described here https://learn.microsoft.com/en-us/onedrive/developer/rest-api/api/shares_get?view=odsp-graph-online#encoding-sharing-urls and then construct a URL like this:

https://api.onedrive.com/v1.0/shares/{encodedUrl}/root/content

Which will download the contents of the shared file at the URL. Option #2 is to splice the URL up and grab the path segment that starts with s! and just use that in the same place as the encoded Url:

https://api.onedrive.com/v1.0/shares/s!.../root/content

Either way, you can then use cURL to download the content of the file.

Upvotes: 11

Related Questions