In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
1. Authorized auth3, authorized address, what is the endpoint url in Chinese? just look at this.
Note the scope permission. Redirect_uri authorization successfully jumps to the address and obtains the code. All auth3 follow this process.
Public String getAuthUrl (Map params0) {String url = Constants.get ("login_url") + "/ authorize"; url + = "? client_id=" + Constants.get ("client_id"); url + = "& response_type=code"; url + = "& redirect_uri=" + Constants.get ("redirect_uri"); url + = "& scope=offline_access%20Files.ReadWrite.All%20Sites.Read.All%20User.Read" Return url;} 2. After obtaining the code, use code to obtain the access_token, in which the key is required.
Code is obtained above, notice that redirect_uri is the same, client_secret key, in the place where the clientid is established
Get the token used by access_token- for api requests. Cache the expiration time of expires_in-token, which is used by refresh_token- to refresh token
Public String getToken (Map params0) {String code = params0.get ("code"); String url = Constants.get ("login_url") + "/ token"; HashMap params = new HashMap (); params.put ("client_id", Constants.get ("client_id")); params.put ("redirect_uri", Constants.get ("redirect_uri")) Params.put ("client_secret", Constants.get ("client_secret")); params.put ("code", code); params.put ("grant_type", "authorization_code"); Map resp = OKHttpUtil.post (url, params); LogUtil.info ("getToken- >" + resp); String token = (String) resp.get ("access_token") Int expires_in = (int) resp.get ("expires_in"); VcodeUtil.timedCache.put ("access_token", token, expires_in* 1000); String refresh_token = (String) resp.get ("refresh_token"); VcodeUtil.timedCache.put ("refresh_token", refresh_token, expires_in* 1000 * 36); return token;} 3. Refresh access_token with refresh_token to establish cache expiration time
Cache access_token for 3600 seconds. Refresh access_token with refresh_token after expiration. The validity of refresh_token is long. Microsoft did not specify how long it will take.
The test is at least above the day level
Public String getTokenByCache (Map params0) {String token = (String) VcodeUtil.timedCache.getNotUpLastAccess ("access_token"); if (token! = null) {return token;} String refresh_token = (String) VcodeUtil.timedCache.getNotUpLastAccess ("refresh_token"); String url = Constants.get ("login_url") + "/ token"; HashMap params = new HashMap () Params.put ("client_id", Constants.get ("client_id")); params.put ("scope", "offline_access Files.ReadWrite.All Sites.Read.All User.Read"); params.put ("refresh_token", refresh_token); params.put ("redirect_uri", Constants.get ("redirect_uri"); params.put ("client_secret", Constants.get ("client_secret")) Params.put ("grant_type", "refresh_token"); Map resp = OKHttpUtil.post (url, params); LogUtil.info ("getToken- >" + resp); token = (String) resp.get ("access_token"); int expires_in = (int) resp.get ("expires_in"); VcodeUtil.timedCache.put ("access_token", token, expires_in* 1000) Refresh_token = (String) resp.get ("refresh_token"); VcodeUtil.timedCache.put ("refresh_token", refresh_token, expires_in* 1000 * 36); return token;} 4. Use access_token to upload files, use simple api, only support 4m files, if it is a large file, use some of the previously recommended tools
/ / PUT / me/drive/items/ {parent-id}: / {filename}: / content, the upload address is difficult to understand. Here I give an example.
It's easy to understand that using absolute path upload, body is directly a file stream.
/ SEARCH_APP/upload/201912/10/Q5pe5A.jpg, this is the file Lujin.
Https://graph.microsoft.com/v1.0/me/drive/root:/SEARCH_APP/upload/201912/10/Q5pe5A.jpg:/content
After a successful upload, the item information will be returned, which contains the download address, save the id, and the path, and provide the itemid download method and path method when downloading.
Public String upload (String uploadPath, String suffix, ByteArrayOutputStream out) throws Exception {byte [] bytes = out.toByteArray (); long id = MD5.md5_long (bytes); Map ins = getIns (id); if (! ins.isEmpty ()) {return (String) ins.get ("itemid");} String date = BaseUtil.getFormatDate (). ReplaceFirst ("/", "); uploadPath + = date String filename = uploadPath + "/" + RandomStringUtils.randomAlphanumeric (6) + "." + suffix; / / PUT / me/drive/items/ {parent-id}: / {filename}: / content String url = "https://graph.microsoft.com/v1.0/me/drive/root:" + filename +": / content "; HttpRequest request = new HttpRequest (url, Method.put); request.setContentType (" image/jpeg ") Request.addHeader ("Authorization", "Bearer" + getTokenByCache (null)); request.setRequestBody (bytes); HttpResponse res = OKHttpUtil.request (request); / / the returned id is itemid. You can use this id to save itemid and filePath String resStr= res.getResponseString (); Map resMap = (Map) OKHttpUtil.deserialize (resStr); String itemid = (String) resMap.get ("id") Return itemid;} 5. Download files using access_token, either itemid or file path
String url = "https://graph.microsoft.com/v1.0/me/drive/items/"+itemid+"/content"; / / press itemid
String url = "https://graph.microsoft.com/v1.0/me/drive/root:" + filepath +": / content "; / / Press File Road strength
Look at the code, with comments, download address, preview address, and sharing address, specific implementation to see oneApi, I have tested
A 302 jump occurs after a successful request. Generally, httpclient will jump on its own. If you don't want to jump, you can find the configuration to get the Location and check the address.
Public Object downLoad (Map params) throws Exception {String itemid = params.get ("id"); / / String url = "https://graph.microsoft.com/v1.0/me/drive/root:" + filepath +": / content "; / / press file Lujin / / String url =" https://graph.microsoft.com/v1.0/me/drive/items/01RHKEMNKSNBGOHRSDPBHJI43LRLM62MV7/preview"; " / / presses itemid// String url = "https://graph.microsoft.com/v1.0/me/drive/items/01RHKEMNKSNBGOHRSDPBHJI43LRLM62MV7/createLink"; / / share by itemid String url =" https://graph.microsoft.com/v1.0/me/drive/items/"+itemid+"/content"; / / Press itemid HttpRequest request = new HttpRequest (url, Method.get); request.addHeader ("Authorization", "Bearer" + getTokenByCache (null)) / / request.setContentType ("application/json"); / / request.setRequestBody ("{\" chromeless\ ":\" true\ "}" .getBytes ()); HttpResponse resp = OKHttpUtil.request (request); / / System.out.println (resp.getResponseString ()); / / System.out.println (resp.getHeader ("Location")); / / 302Jump to automatically retrieve the image URL return resp.getRespInputsStream () } 6. It's done here. Since you have to download it every time, make your own browser cache, plus cloudflare cdn, it's OK to use.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.