0x00 Introduction
Microsoft OneDrive has long been a indispensable cloud service for me to store PPTs, PDFs, etc. Its user experience are marvelous, the sync, upload and download speed are satisfying, but what bothers me is that it takes me a long time to fully load a OneDrive file share page, as a result, I seldom use OneDrive to share files with my friends.
0x01 Methods
It is quite simple to obtain direct download link for personal OneDrive link like
1 | https://1drv.ms/u/s!AnN4R6WWGczehw51hTeeCjrbL_4O |
Just replace the “1drv.ms” with “1drv.ws“, the new link is what we want.In this case, the direct download link should be:
1 | https://1drv.ws/u/s!AnN4R6WWGczehw51hTeeCjrbL_4O. |
As for personal OneDrive link after redirection like
1 | https://onedrive.live.com/redir.aspx?resid=DECC1996A5477873!723&authkey=!AqWkl_GohQTI94Y |
We need to obtain two credentials in the query string, which is “resid” and “authkey “, in this case, they are “DECC1996A5477873!723” and “!AqWkl_GohQTI94Y” respectively. After gathering the credentials, the direct link can be generated like following form:
1 | http://storage.live.com/items/{resid}?authkey={authkey} |
In this case, the direct download link should be:
1 | http://storage.live.com/items/DECC1996A5477873!723?authkey=!AqWkl_GohQTI94Y |
Then the Office 365 OneDrive link like:
1 | https://xxx-my.sharepoint.com/:b:/g/personal/xx_xxx_onmicrosoft_com/blah-blah |
We also need to extract three credentials in the link, which is “domain“, “resid” and “user_info“, in this case, they are “https://xxx-my.sharepoint.com“, “blah-blah” and “xx_xxx_onmicrosoft_com” respectively. After getting the credentials, the direct download link can be generated by following pattern:
1 | {domain}/personal/{user_info}/_layouts/15/download.aspx?share={resid} |
In this case, the direct download link should be:
1 | https://xxx-my.sharepoint.com/personal/xx_xxx_onmicrosoft_com/_layouts/15/download.aspx?share=blah-blah |
0x02 Code example
The following code is a example Java code to show you how to generate a OneDrive direct link from OneDrive share link.
Its new update can be found on https://github.com/Xiderowg/OneDriveDirectLinkHelper
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | package cn.edlinus; import java.util.*; import java.io.*; import java.net.URLDecoder; import java.net.URL; import java.awt.Toolkit; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.Transferable; public class Main { public static void main(String[] args) { System.out.println("-----------------------------------------------------------------------------------------"); System.out.println("| One Drive Direct Link Helper ver 0.1 |"); System.out.println("-----------------------------------------------------------------------------------------"); BufferedReader scanner = new BufferedReader(new InputStreamReader(System.in)); String str = ""; while (true) { // Get user input try { System.out.println("Please input or paste the link here, type in 'q' will terminate the program:"); str = scanner.readLine(); } catch (IOException ex) { ex.printStackTrace(); } if (str.equals("q")) break; // Generate the direct link String dLink = ""; if (str.length() > 0) { // Judge the link type if (str.contains("1drv.ms")) { // Personal OneDrive link try { dLink = get1drv(str); } catch (Exception e) { System.out.println("Url recognized, but it is doesn't meet the standard, the standard url pattern should be like:"); System.out.println("https://1drv.ms/u/s!AnN4R6WWGczehw51hTeeCjrbL_4O"); //e.printStackTrace(); continue; } } else if (str.contains("onedrive.live.com")) { // Personal OneDrive link after redirection try { dLink = getLiveCom(str); } catch (Exception e) { System.out.println("Url recognized, but it is doesn't meet the standard, the standard url pattern should be like:"); System.out.println("https://onedrive.live.com/redir.aspx?cid=decc1996a5477873&resid=DECC1996A5477873!723&parId=DECC1996A5477873!700&authkey=!AqWkl_GohQTI94Y&Bsrc=SMIT&ref=button"); //e.printStackTrace(); continue; } } else if (str.contains("sharepoint.com")) { // Office 365 OneDrive link try { dLink = getSharepoint(str); } catch (Exception e) { System.out.println("Url recognized, but it is doesn't meet the standard, the standard url pattern should be like:"); System.out.println("https://guanghou-my.sharepoint.com/:v:/g/personal/rj6ayxdzw_get365_pw/EU9uT2CgbqdJrZOmm9FFpXcB6dso8KvS6n6RXfrK_B0e2Q?e=umxSJ4"); //e.printStackTrace(); continue; } } else { System.out.println("Unrecognized url, Please check again."); continue; } System.out.println("The direct link is: " + dLink); setClipboardString(dLink); System.out.println("The link has been copy to clipboard."); } } try { scanner.close(); } catch (Exception e) { e.printStackTrace(); } } /** * Link like https://1drv.ms/u/s!AnN4R6WWGczehw51hTeeCjrbL_4O * Personal * * @param link * @return Direct Link * @throws Exception */ public static String get1drv(String link) throws Exception { // Replace 1drv.ms with 1drv.ws return link.replaceFirst("1drv.ms", "1drv.ws"); } /** * Link like https://onedrive.live.com/redir.aspx?cid=decc1996a5477873&resid=DECC1996A5477873!723&parId=DECC1996A5477873!700&authkey=!AqWkl_GohQTI94Y&Bsrc=SMIT&ref=button * Personal * * @param link * @return Direct Link * @throws Exception */ public static String getLiveCom(String link) throws Exception { // Get resID and authKey first String resID, authKey; Map<String, String> queryParams = getQueryParams(link); resID = queryParams.get("resid"); authKey = queryParams.get("authkey"); // Generate the direct link StringBuilder sb = new StringBuilder("http://storage.live.com/items/"); sb.append(resID); sb.append("?authkey="); sb.append(authKey); return sb.toString(); } /** * Link like https://xxx-my.sharepoint.com/:b:/g/personal/xx_xxx_onmicrosoft_com/blah-blah * Office 365 * * @param link * @return * @throws Exception */ public static String getSharepoint(String link) throws Exception { // Get domain and path URL url = new URL(link); String path = url.getPath(); String query = url.getQuery(); String domain = link.substring(0, link.length() - path.length() - query.length() - 1); // Extract user info String[] tmp = path.split("/"); String user_info = tmp[4], resID = tmp[5]; // Return the direct link StringBuilder sb = new StringBuilder(domain); sb.append("personal/"); sb.append(user_info); sb.append("/_layouts/15/download.aspx?share="); sb.append(resID); return sb.toString(); } /** * Copy text to Clipboard * * @param text */ public static void setClipboardString(String text) { // Get the system's Clipboard Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); // Package the text Transferable trans = new StringSelection(text); // Set the contents to Clipboard clipboard.setContents(trans, null); } /** * Get Query Parameters from a url * * @param url * @return Query Parameters */ public static Map<String, String> getQueryParams(String url) { try { Map<String, String> params = new HashMap<>(); String[] urlParts = url.split("\\?"); if (urlParts.length > 1) { String query = urlParts[1]; for (String param : query.split("&")) { String[] pair = param.split("="); String key = URLDecoder.decode(pair[0], "UTF-8"); String value = ""; if (pair.length > 1) { value = URLDecoder.decode(pair[1], "UTF-8"); } params.put(key, value); } } return params; } catch (UnsupportedEncodingException ex) { throw new AssertionError(ex); } } } |