Self built free drawing bed, free resources use onedriver api self built drawing bed

Keywords: Programming Google JSON

Free picture bed, recommended stable

Here is the recommendation. It has been summarized Free bed point here

It's not easy to use others' worries, and it's not flexible to connect the api. It's mainly to be a station for its own needs

Provide an idea to use onedriver or google driver api to build a free drawing bed

cocook free picture bed

  • It doesn't matter what language it is
  • With servers, the most basic configuration is OK
  • Understand oneDriver api. Now the old api authorization is not easy to use. It is recommended to use the latest one directly
  • Register clintid address Registration point here
  • api address, mainly authorize (use auth2 latest version) upload and download api point here.
  • Because Microsoft has too many APIs, it took two days to complete the docking. It is recommended to use the latest api

Here is the specific process

1. Authorization auth2. Authorization address. What is the endpoint url in Chinese? Just look at this

Pay attention to the scope permission, redirect \ u URI authorization successfully jumps to the address, gets the code, and all auth2 is this process

    public String getAuthUrl(Map<String, String> 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 the code to obtain the access_token, in which the key is required

The code is obtained from the above. Note that the redirect URI is the same, and the client secret key is in the place where the clientid is established
Get access_token - token used for api request, expires_in - token expiration time cache, refresh_token - used to refresh token

    public String getToken(Map<String, String> params0){
        String code = params0.get("code");
        String url = Constants.get("login_url") +"/token";
        HashMap<String, String> 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<String, Object> 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 and set cache expiration time

Cache access_token for 3600 seconds, and refresh access_token with refresh_token after invalidation. Refresh_token has a long time effect. Microsoft did not specify a specific time,
Test at least above day level

    public String getTokenByCache(Map<String, String> 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<String, String> 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<String, Object> 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 4M files are supported. If it is a large file, use some tools recommended before

//Put / me / drive / items / {parent ID}: / {filename}: / content, upload address is very confusing, here I do an example
So it's easy to understand. Use absolute path to upload. body is a file stream directly
/SEARCH_APP/upload/201912/10/Q5pe5A.jpg here is file Lujin
https://graph.microsoft.com/v1.0/me/drive/root:/SEARCH_APP/upload/201912/10/Q5pe5A.jpg:/content
After the upload is successful, the information of this item will be returned. There is a download address in it. Save the id and path. When downloading, provide the download method and path method of itemid

    public String upload(String uploadPath, String suffix, ByteArrayOutputStream out) throws Exception{
        byte[] bytes = out.toByteArray();

        long id = MD5.md5_long(bytes);
        Map<String, Object> 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, which can be used for some operations to save itemid and filePath
        String resStr=  res.getResponseString();
        Map<String, Object> resMap =  (Map<String, Object>) OKHttpUtil.deserialize(resStr);
        String itemid = (String) resMap.get("id");

        return itemid;
    }

5. Download the file using access_token, either using itemid or the file path

String url = "https://graph.microsoft.com/v1.0/me/drive/items/"+itemid+"/content"; // Download by itemid
String url = "https://graph.microsoft.com/v1.0/me/drive/root:" + filepath +":/content"; // According to the document
Look at code, It's annotated, Download address, Preview address, And share address, Concrete realization oneApi, I tested them all
302 jump after successful request, commonly httpclient I can dance by myself, Do not want to jump to find configuration to get Location Address can be viewed

        public Object downLoad(Map<String, String> params) throws Exception{
        String itemid = params.get("id");
//      String url = "https://graph.microsoft.com/v1.0/me/drive/root:" + filepath +":/content"; / / press file strength
//      String url = "https://graph.microsoft.com/v1.0/me/drive/items/01rhkemnksngbohrsdpbhji43lrlm62mv7 / Preview"; / / preview press itemid
//      String url = "https://graph.microsoft.com/v1.0/me/drive/items/01rhkemnksngbohrsdpbhji43lrlm62mv7 / createlink"; / / share by itemid
        String url = "https://graph.microsoft.com/v1.0/me/drive/items/"+itemid+"/content "; / / download by 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")); / / 302 jump to automatically retrieve the image URL
        return resp.getRespInputsStream();
    }

6. It's finished here. Since you have to download it every time, make a browser cache and add cloudflare cdn. It's OK to use

cocook picture bed

Promote online search of film and television, ethics, American drama, animation cocook Internet Movie search
Promote daily hot spots cncknews

Posted by Misery_Kitty on Wed, 04 Mar 2020 04:19:27 -0800