Code Monkey home page Code Monkey logo

baidupansdk's Introduction

百度云盘 Java SDK

已实现功能:

  • 授权,获取 accessToken、refreshToken
  • 上传文件
  • 分享文件
  • 查询文件列表
  • 查询文件详情

使用

  1. Maven 引用,最新版本为 https://mvnrepository.com/artifact/net.peihuan/baidu-pan-starter
<dependency>
    <groupId>net.peihuan</groupId>
    <artifactId>baidu-pan-starter</artifactId>
    <version>1.0.1</version>
</dependency>
  1. 修改application.yaml文件,添加配置
baidu-pan:
  client-id: "your client id"
  device-id: 1111111
  client-secret: "your secret"
  share-third-id: 111                   # 不需要分享则无需填写
  share-secret: "secret"                # 不需要分享则无需填写
  root-dir: "/apps/your/root/path/"     # 文件上传的根目录
  1. 注入 BaiduService 后配置持久化方式,即可使用:
@Component
public class BaiduTest {
    @Autowired
    private BaiduService baiduService;

    @PostConstruct
    public void init() {
        // 必须配置持久化,不配置默认为内存实现
        // TODO BaiduOps 需要用户基于 mysql or redis 来实现
        BaiduOps ops = new RedisOpsImpl();
        BaiduOAuthConfigStorage baiduOAuthConfigStorage = new BaiduOAuthConfigServiceImpl(ops);
        baiduService.setConfigStorage(baiduOAuthConfigStorage);
    }

    public void getAuthorizeUrl() {
        // 由于可能多个用户授权使用,state 可用于传递 userId
        System.out.println(baiduService.getAuthorizeUrl("http://your.redirct.com", "your state"));
    }

    public void getAuthorizeUrl(String code) {
        // 任何方法都需要传递 userId 来标记这个 code 属于哪个用户。
        // 如果该系统只有开发者一个人需要授权百度云盘,userId 填写任意一个固定值即可
        // 调用此方法后,内部会持久化 accessToken 和 refreshToken
        System.out.println(baiduService.getAccessTokenByCode("1", code, "http://your.redirct.com"));
    }

    public void upload(File file) {
        // 任何方法都需要传递 userId
        // userId 为 1 的用户上传一个文件至 /apps/your/root/path/object/path
        baiduService.getPanService().uploadFile("1", "object/path", file, RtypeEnum.OVERRIDE);
    }
}

其他: 如果你不用 Spring 框架,也可以自己 new 一个 baiduService 来使用:

BaiduService baiduService = new BaiduServiceImpl(properties, okHttpClient);
// config your
baiduService.setConfigStorage(baiduOAuthConfigStorage);

baidupansdk's People

Contributors

peihuanhuan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.