0

I want to generate google drive direct download link without opening web page.

I have found 1 site which do same thing but I did not find how he is doing.

below is the site which generates direct download link without opening google drive page.

https://links-safety.com/download.php?id=0B475ByfcR9n4a1JMVEZxQno2Tmc

0B475ByfcR9n4a1JMVEZxQno2Tmc is google file and replace with any file.

can anyone tell me how can I do that?I want to make same page like above site.

I tried this url but its not working. instead of starting download it opens page.

https://drive.google.com/uc?id=0BwSYfbOPSw89Rno4LTZpSGF6RUE

emma worksy
  • 21
  • 1
  • 4

1 Answers1

3

A quick look at the source of the site, it generates the following javascript.

<script>
    setTimeout(function() {
        window.location.href = "https://doc-0o-a0-docs.googleusercontent.com/docs/securesc/ha0ro937gcuc7l7deffksulhg5h7mbp1/nemv3kbhggb02fn2933p80ec5vsi521t/1512345600000/10410701494873540224/*/0B475ByfcR9n4a1JMVEZxQno2Tmc?e=download";
    }, 300);
</script>

But where does that url come from?

With a little fiddling and watching the console on when it does ask to confirm on google, you can see it does a POST request and returns some json.

)]}'
{"disposition":"SCAN_CLEAN","downloadUrl":"https://doc-0c-9k-docs.googleusercontent.com/docs/securesc/aonp7ed1gjmns5tl5di4fl0psa1cppk8/16panrhf9etu7sqgddkaduij5anokf8t/1512345600000/17294955007197410767/17294955007197410767/0ByzJffaEk18uN2ZLeGlIRGVOaDJmWS1WU1RUN3dXUGdtUUx3?e\u003ddownload","fileName":"install.sh","scanResult":"OK","sizeBytes":4936}

So just mock that with PHP

Make a json POST request to that url, google will respond with the json, then just strip out )]}' json decode it, then use a header to redirect to the file.

<?php
$id = '0B475ByfcR9n4a1JMVEZxQno2Tmc';

$ch = curl_init('https://drive.google.com/uc?id='.$id.'&export=download');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, []);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    ... see notice below
));                                                                                                                                                                          
$result = curl_exec($ch);

$object = json_decode(str_replace(')]}\'', '', $result));

exit(header('Location: '. $object->downloadUrl));

Edit (08-04-18)

Looks like some additional headers have been added, if missing it will throw a 400 Bad Request. No biggie, it's still easy to mock it by looking at the request headers when downloading a file from your own drive. I'm unwilling to share a copy&paste solution, as the above still works you just need to add some headers and StackOverflow is not a free coding service nor am I required to maintain every answer I've ever written. Good luck.

Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
  • your welcome, the given php is very crude so you should definitely add some checks in it to avoid situations when `$result` or `$object->downloadUrl` is empty. happy coding. – Lawrence Cherone Dec 04 '17 at 16:53
  • Download file size is unknown although file size. how to solve it? i want to add custom headers like header('Content-type: application/zip'); – emma worksy Jan 08 '18 at 15:18
  • i want to header('Content-Length: ' . mb_strlen($content)) and header('Content-Type: application/octet-stream'); in the code – emma worksy Jan 08 '18 at 15:28
  • Where is `$content` set in the above code? Its not. This code does not download the file it simply redirects to the download location. If you want to proxy the file you need to fetch it first (on the server) then you can use that header. Please open a new question if you have a new question. – Lawrence Cherone Jan 08 '18 at 15:30
  • @LawrenceCherone this script not working anymore. is there another way? – Jazuly May 24 '18 at 12:44
  • @Jazuly Yes I know, read the note at bottom of post, its still possible to do it but you will need to mock the request, including sending some additional headers. – Lawrence Cherone May 25 '18 at 03:13
  • @LawrenceCherone but this one not support if the file already QUOTA_EXCEEDED. – Jazuly May 25 '18 at 10:14
  • if using this one working even if the file is QUOTA_EXCEEDED https://links-safety.com/download.php?id=id_file – Jazuly May 25 '18 at 10:56