doukuangxiu1621 2017-12-04 01:20
浏览 45
已采纳

生成谷歌驱动器链接的自定义直接下载链接

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

  • 写回答

1条回答 默认 最新

  • douhanshu5517 2017-12-04 01:54
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)