一文搞懂Linux命令行下载OneDrive分享文件
什么问题?
因为OneDrive有些坑,无法从分享界面获取真实下载链接,比如下面这个链接:
https://connecthkuhk-my.sharepoint.com/:f:/g/personal/jhyang13_connect_hku_hk/EsEgHtGOWbJImxop6tF15FIBIH-ihrjuDclbrbmwWfY_RA?e=s6fitN
如果直接在服务器里 wget 这个下载链接,100%会403: Forbidden。
怎么办?
有经验的老手都知道肯定是没带 Cookie ,但是要怎么带Cookie呢,其实很简单。
Step1:
请使用Chrome,首先你需要按下F12,打开开发人员工具,然后转到网络选项。
Step2:
之后点击想要下载的文件(注意!只能点一个文件,不能多个文件进行下载),同时观察开发人员工具窗口,找到带有download.aspx/?…. 的那个链接。
Step3:
之后在那个链接上右键,选择 复制-复制为cURL命令(bash)。会在剪切板出现类似下面的命令语句:
curl 'https://connecthkuhk-my.sharepoint.com/personal/jhyang13_connect_hku_hk/_layouts/15/download.aspx?UniqueId=47909841%2D4653%2D4b93%2Da33f%2D91ec825a7526' \
-H 'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7' \
-H 'accept-language: zh-CN,zh;q=0.9' \
-H 'cookie: MicrosoftApplicationsTelemetryDeviceId=24cabc4c-c68c-4f63-8877-ae395ced04e7; MSFPC=GUID=c4e909b5f753492d9753ad789b5bfc42&HASH=c4e9&LV=202309&V=4&LU=1694955555622; FedAuth=77u/PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz48U1A+VjEzLDBoLmZ8bWVtYmVyc2hpcHx1cm4lM2FzcG8lM2Fhbm9uIzY5ODc0ZDJhYmVkZGUyMWU5OWQzM2MyYWMxNGIyYmMxNTk1MTlmZjViM2E0Y2M4ZmVkMjcwNTZlODIyNmQ1NjMsMCMuZnxtZW1iZXJzaGlwfHVybiUzYXNwbyUzYWFub24jNjk4NzRkMmFiZWRkZTIxZTk5ZDMzYzJhYzE0YjJiYzE1OTUxOWZmNWIzYTRjYzhmZWQyNzA1NmU4MjI2ZDU2MywxMzM2MzYyNzAxNjAwMDAwMDAsMCwxMzM2Mzc2NzEyMDIxNDYzMDUsMC4wLjAuMCwyNTgsZTgwZDhlNzUtNTJiOS00ODM5LWEzNTgtODdhYmI5M2IzNTY3LCwsZjk3YTM1YTEtZTBkMi0zMDAwLTg1N2YtZmUxYjJjZTMyNWIxLDdhYWUzNWExLTcwNTQtMzAwMC04NTdmLWYzODgxYTY1MGNlNix3d2ZoSmdNQkhVS3J6TjA0eHlNM2V3LDAsMCwwLCwsLDI2NTA0Njc3NDM5OTk5OTk5OTksMCwsLCwsLCwwLCwxOTYwMDAsRGFEQWZqUVFtcHlPWHgyUnJLX1c1bHZvTFo0LGNMSzhJT3lCem96WjZLZ0FOcHU5d1ZwVnR5eVNFcDQ2dTBlYmhDcjQvcGlnMVdJaVN2eGdRK2FZbUV1WkhiaUkyWjdNcTJ1ejZ0a2pRMTJmeGVndUtnVElzVDFKY1BGT0VQdnJRRnF1emFYWklPakw4dXdBK3QrbUE2TnRxdFpTSnVud1RweSt5RVZ4bU5ZdnllLzJXaXJJVFJlN0dUa3A4NGpzdE1KN0k1c1AvZHdZb1JOSUFSWFdseDFtMGpvTk8xcXE2LzhHNzhHUU12S2JCQzJYaEpHWWFyekRFVTVlL0RYK1VPa0tjclNlcTRlWDY3aStuNUNRMTliZEI5dzVBUFNnM2hFUzZGV2xxZHp5SlgyVWgzaDJ5eU0wcWRveUd1c2twakpPYTNlK0l4bEtRZFVHUnVpN29Kc3NmSkdwbENsVExTbEZsV1BQb3dMNlhHSE5mUT09PC9TUD4=; WSS_FullScreenMode=false' \
-H 'priority: u=0, i' \
-H 'referer: https://connecthkuhk-my.sharepoint.com/personal/jhyang13_connect_hku_hk/_layouts/15/onedrive.aspx?id=%2Fpersonal%2Fjhyang13%5Fconnect%5Fhku%5Fhk%2FDocuments%2Fpretrained%5Fmodels%2Fregionplc%2Fcaption%5Ffiles%2Fscannet&ga=1' \
-H 'sec-ch-ua: "Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126"' \
-H 'sec-ch-ua-mobile: ?0' \
-H 'sec-ch-ua-platform: "Windows"' \
-H 'sec-fetch-dest: iframe' \
-H 'sec-fetch-mode: navigate' \
-H 'sec-fetch-site: same-origin' \
-H 'sec-fetch-user: ?1' \
-H 'service-worker-navigation-preload: {"supportsFeatures":[]}' \
-H 'upgrade-insecure-requests: 1' \
-H 'user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36'
Step4:
之后粘贴到Linux的命令行里,最后在后面补加一句 --output xxx ,其中 xxx 是想要保存的文件名。请注意,如果文件的名字过长像复制名字,可以在step2中点了下载后选择保存路径时复制文件的名字。最终执行的命令就是类似这样的:
curl 'https://connecthkuhk-my.sharepoint.com/personal/jhyang13_connect_hku_hk/_layouts/15/download.aspx?UniqueId=47909841%2D4653%2D4b93%2Da33f%2D91ec825a7526' \
-H 'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7' \
-H 'accept-language: zh-CN,zh;q=0.9' \
-H 'cookie: MicrosoftApplicationsTelemetryDeviceId=24cabc4c-c68c-4f63-8877-ae395ced04e7; MSFPC=GUID=c4e909b5f753492d9753ad789b5bfc42&HASH=c4e9&LV=202309&V=4&LU=1694955555622; FedAuth=77u/PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz48U1A+VjEzLDBoLmZ8bWVtYmVyc2hpcHx1cm4lM2FzcG8lM2Fhbm9uIzY5ODc0ZDJhYmVkZGUyMWU5OWQzM2MyYWMxNGIyYmMxNTk1MTlmZjViM2E0Y2M4ZmVkMjcwNTZlODIyNmQ1NjMsMCMuZnxtZW1iZXJzaGlwfHVybiUzYXNwbyUzYWFub24jNjk4NzRkMmFiZWRkZTIxZTk5ZDMzYzJhYzE0YjJiYzE1OTUxOWZmNWIzYTRjYzhmZWQyNzA1NmU4MjI2ZDU2MywxMzM2MzYyNzAxNjAwMDAwMDAsMCwxMzM2Mzc2NzEyMDIxNDYzMDUsMC4wLjAuMCwyNTgsZTgwZDhlNzUtNTJiOS00ODM5LWEzNTgtODdhYmI5M2IzNTY3LCwsZjk3YTM1YTEtZTBkMi0zMDAwLTg1N2YtZmUxYjJjZTMyNWIxLDdhYWUzNWExLTcwNTQtMzAwMC04NTdmLWYzODgxYTY1MGNlNix3d2ZoSmdNQkhVS3J6TjA0eHlNM2V3LDAsMCwwLCwsLDI2NTA0Njc3NDM5OTk5OTk5OTksMCwsLCwsLCwwLCwxOTYwMDAsRGFEQWZqUVFtcHlPWHgyUnJLX1c1bHZvTFo0LGNMSzhJT3lCem96WjZLZ0FOcHU5d1ZwVnR5eVNFcDQ2dTBlYmhDcjQvcGlnMVdJaVN2eGdRK2FZbUV1WkhiaUkyWjdNcTJ1ejZ0a2pRMTJmeGVndUtnVElzVDFKY1BGT0VQdnJRRnF1emFYWklPakw4dXdBK3QrbUE2TnRxdFpTSnVud1RweSt5RVZ4bU5ZdnllLzJXaXJJVFJlN0dUa3A4NGpzdE1KN0k1c1AvZHdZb1JOSUFSWFdseDFtMGpvTk8xcXE2LzhHNzhHUU12S2JCQzJYaEpHWWFyekRFVTVlL0RYK1VPa0tjclNlcTRlWDY3aStuNUNRMTliZEI5dzVBUFNnM2hFUzZGV2xxZHp5SlgyVWgzaDJ5eU0wcWRveUd1c2twakpPYTNlK0l4bEtRZFVHUnVpN29Kc3NmSkdwbENsVExTbEZsV1BQb3dMNlhHSE5mUT09PC9TUD4=; WSS_FullScreenMode=false' \
-H 'priority: u=0, i' \
-H 'referer: https://connecthkuhk-my.sharepoint.com/personal/jhyang13_connect_hku_hk/_layouts/15/onedrive.aspx?id=%2Fpersonal%2Fjhyang13%5Fconnect%5Fhku%5Fhk%2FDocuments%2Fpretrained%5Fmodels%2Fregionplc%2Fcaption%5Ffiles%2Fscannet&ga=1' \
-H 'sec-ch-ua: "Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126"' \
-H 'sec-ch-ua-mobile: ?0' \
-H 'sec-ch-ua-platform: "Windows"' \
-H 'sec-fetch-dest: iframe' \
-H 'sec-fetch-mode: navigate' \
-H 'sec-fetch-site: same-origin' \
-H 'sec-fetch-user: ?1' \
-H 'service-worker-navigation-preload: {"supportsFeatures":[]}' \
-H 'upgrade-insecure-requests: 1' \
-H 'user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36'
--output caption_detic-template_and_kosmos_125k_iou0.2.json
该方法局限性?
注意!只能点一个文件,不能多个文件进行下载
这个方法多个文件点击下载F12中不会出现带有download.aspx/?…. 的那个链接,因此尚未探索出多文件和文件夹下载的方法,如果大家有好的方法可以在评论区留言,我尝试成功后更新此博客!
参考博客:Linux命令行下载OneDrive分享链接中的文件