Skip to content

Instantly share code, notes, and snippets.

@lcherone
Created April 8, 2018 12:05
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lcherone/91fadc5411fa08d97e6bdb556ea97c6d to your computer and use it in GitHub Desktop.
Save lcherone/91fadc5411fa08d97e6bdb556ea97c6d to your computer and use it in GitHub Desktop.
google.php
<?php
$id = '0B475ByfcR9n4a1JMVEZxQno2Tmc';
$ch = curl_init('https://drive.google.com/uc?id='.$id.'&confirm=jYel&authuser=0&export=download');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, []);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: */*',
'Accept-encoding: gzip, deflate, br',
'Accept-language: en-GB,en-US;q=0.9,en;q=0.8',
'Content-length: 0',
'Content-type: application/x-www-form-urlencoded;charset=UTF-8',
'Origin: https://drive.google.com',
'Referer: https://drive.google.com/drive/my-drive',
'User-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36',
'X-chrome-connected: id=102224796319835333482,mode=0,enable_account_consistency=false',
'X-client-data: CIa2yQEIpbbJAQipncoBCKijygEYkqPKAQ==',
'X-drive-first-party: DriveWebUi',
'X-json-requested: true',
));
$result = curl_exec($ch);
print_r($result);
$object = json_decode(str_replace(')]}\'', '', $result));
//exit(header('Location: '. $object->downloadUrl));
@Anan5a
Copy link

Anan5a commented Sep 9, 2018

&confirm=jYel 

part came from?

@dawsbot
Copy link

dawsbot commented Aug 28, 2019

I'm getting an error "UNAUTHORIZED" after duplicating this into postman. Have the headers required by Google changed?:

image

@lcherone
Copy link
Author

lcherone commented Aug 28, 2019

@dawsbot id is different :/

Try mocking the request again, It shows how I got to whats needed in the answer on SO https://stackoverflow.com/questions/47625214/generate-custom-direct-download-link-for-google-drive-link/47625446#47625446

@dawsbot
Copy link

dawsbot commented Aug 28, 2019

Thank you for the rapid response here. I switched in my own ID here in order to test it with our real use-case since I was finding your example ID invalid.

But was that a mistake, should the provided ID "0B475ByfcR9n4a1JMVEZxQno2Tmc" still work?

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment