千家信息网

如何使用onedriver api自建图床?

发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,1. 授权 auth3, 授权地址, 中文叫什么 终结点url, 看这个就知道了注意 scope 权限 , redirect_uri 授权成功跳转地址, 获取code, 所有的 auth3 都是这个流
千家信息网最后更新 2026年11月01日如何使用onedriver api自建图床?

1. 授权 auth3, 授权地址, 中文叫什么 终结点url, 看这个就知道了

注意 scope 权限 , redirect_uri 授权成功跳转地址, 获取code, 所有的 auth3 都是这个流程

    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. 获取code后, 使用code 获取 access_token, 其中需要用到密钥

code是上面获取到的, 注意 redirect_uri 相同 , client_secret 密钥, 在建立clientid的地方
获取到 access_token- 用于api请求的token, expires_in - token过期时间缓存一下, refresh_token-刷新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_token 刷新 access_token,建缓存过期时间

缓存 access_token 3600秒, 失效后使用 refresh_token 刷新 access_token , refresh_token的 时效较长, 微软没有指定具体多久,
测试 至少在 天级别以上

    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. 使用 access_token 上传文件, 使用简易api, 只支持 4M的文件, 如果是大文件的话, 还是用以前推荐的一些工具吧

//PUT /me/drive/items/{parent-id}:/{filename}:/content , 上传地址很费解, 这里我做了例子
这样好理解一点, 使用 绝对路径上传, body 直接是文件流
/SEARCH_APP/upload/201912/10/Q5pe5A.jpg 这里是文件路劲
https://graph.microsoft.com/v1.0/me/drive/root:/SEARCH_APP/upload/201912/10/Q5pe5A.jpg:/content
上传成功后 会返回 这个 item 的信息, 里面有下载地址, 保存这个 id, 和 路径, 下载的时候提供 itemid 下载方式 和路径方式

    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);        // 返回的 id 就是 itemid, 可以用此id做一些操作 保存itemid 和 filePath        String resStr=  res.getResponseString();        Map resMap =  (Map) OKHttpUtil.deserialize(resStr);        String itemid = (String) resMap.get("id");        return itemid;    }

5. 使用 access_token 下载文件, 使用itemid 或 文件路径都可以

String url = "https://graph.microsoft.com/v1.0/me/drive/items/"+itemid+"/content"; // 下載 按itemid
String url = "https://graph.microsoft.com/v1.0/me/drive/root:" + filepath +":/content"; // 按文件路劲
看代码, 上面 带了注释, 下载地址, 预览地址, 和分享地址, 具体实现 看 oneApi, 我都测试过
成功请求后会出现 302 跳转, 一般 httpclient 都会自己跳, 不想跳的找配置 获取Location 可以查看地址

        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"; // 按文件路劲//      String url = "https://graph.microsoft.com/v1.0/me/drive/items/01RHKEMNKSNBGOHRSDPBHJI43LRLM62MV7/preview";  // 預覽按itemid//      String url = "https://graph.microsoft.com/v1.0/me/drive/items/01RHKEMNKSNBGOHRSDPBHJI43LRLM62MV7/createLink"; // 分享按itemid        String url = "https://graph.microsoft.com/v1.0/me/drive/items/"+itemid+"/content";  // 下載 按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 跳转, 自动重新获取图片 URL        return resp.getRespInputsStream();    }

6. 到这里就完成了, 由于每次都要下载, 自己做个浏览器缓存, 再加上 cloudflare cdn, 使用起来还行

文件 地址 缓存 路径 成功 密钥 方式 时间 测试 相同 费解 代码 例子 信息 图片 地方 就是 工具 时候 时效 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 山东特亿宝互联网科技有限公司 互联网佳科技有限公司 互联网科技未来 麻工互联网科技 互联网科技作文 广西互联网科技公司 互联网科技美容 北京大学互联网科技怎么样 广州恒生互联网科技有限 宝灵互联网科技有限公司 互联网科技公司有风险吗 坤平互联网科技 西安小鹰速贷互联网科技有限公司 高科技互联网发展前景如何 烟台灵鸽互联网科技有限公司 小米互联网科技园入驻 下一代互联网重大应用科技园 江苏互联网科技小贷 西安左小互联网科技有限公司 广州全民伙伴互联网科技有限公司 新华互联网科技学校 关于新基建和互联网科技 北京互联网科技公司 互联网汽车科技小镇 互联网科技实习生岗位职责 汽车互联网创新科技园 互联网科技产业特征 企晟互联网信息科技有限公司 玉溪新华互联网科技好不好 朱啸虎曾投资哪些互联网科技企业
0