Reputation: 21
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
Upvotes: 0
Views: 5726
Reputation: 46602
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.
Upvotes: 3