初始化

This commit is contained in:
je 2023-08-01 07:57:14 +08:00
commit 670f5baf03
9 changed files with 376 additions and 0 deletions

33
.gitignore vendored Normal file
View File

@ -0,0 +1,33 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/

81
pom.xml Normal file
View File

@ -0,0 +1,81 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.jecyp</groupId>
<artifactId>baidu-pan-openapi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>baidu-pan-openapi</name>
<description>baidu-pan-openapi</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.1</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.16</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,13 @@
package cn.jecyp.baidupanopenapi;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class BaiduPanOpenapiApplication {
public static void main(String[] args) {
SpringApplication.run(BaiduPanOpenapiApplication.class, args);
}
}

View File

@ -0,0 +1,115 @@
package cn.jecyp.baidupanopenapi.controller;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.jecyp.baidupanopenapi.pojo.dto.BaiduPanAccessTokenDTO;
import cn.jecyp.baidupanopenapi.pojo.dto.BaiduPanImageListDTO;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/**
* @author cyp
* @date 2023-07-31
*/
@Controller
@RequestMapping("/baidu/pan")
public class BaiduPanController {
private String callback = "http://jecyp.nat300.top/openapi/baidu/pan/callback";
private BaiduPanAccessTokenDTO accessToken = new BaiduPanAccessTokenDTO();
{
accessToken.setAccess_token("121.62804fe376fc92c5ac1ddd017c056edc.YmfpvVDzVgESpQp2wgw1yVfUd23vHbAIzhONRXp.795eYw");
accessToken.setExpires_in("2592000");
accessToken.setRefresh_token("122.ef800517f78d73f18acf5126b5659631.YaxBybnvqW6pUiTafTJCN5atxmnTaOu8U8QV6e-.f6WXCw");
accessToken.setScope("basic netdisk");
}
@RequestMapping("/callback")
public String callback(@RequestParam(name = "code", required = false, defaultValue = "code") String code,
@RequestParam(name = "state", required = false, defaultValue = "state") String state,
HttpServletRequest request){
String contextPath = request.getContextPath();
System.out.println("contextPath:"+contextPath);
System.out.println("code="+code+",state="+state);
String accessTokenBaseUrl = "https://openapi.baidu.com/oauth/2.0/token?grant_type=authorization_code&code={3}&client_id={0}&client_secret={1}&redirect_uri={2}";
String accessTokenUrl = MessageFormat.format(accessTokenBaseUrl, "4DyoVFaBH2zoSAnpAowWS5w3P6FsgGM1", "ePuUZ64mq5qhNcRL7UpcRUhUoZVeHUZl",callback,code);
String accessTokenRes = HttpUtil.get(accessTokenUrl);
System.out.println("refreshTokenRes="+accessTokenRes);
BaiduPanAccessTokenDTO baiduPanAccessTokenDTO = JSONUtil.toBean(accessTokenRes, BaiduPanAccessTokenDTO.class);
System.out.println("refreshTokenRes bean="+baiduPanAccessTokenDTO);
// String refreshTokenBaseUrl = "https://openapi.baidu.com/oauth/2.0/token?grant_type=refresh_token&refresh_token={2}&client_id={0}&client_secret={1}";
// String refreshTokenUrl = MessageFormat.format(refreshTokenBaseUrl, "4DyoVFaBH2zoSAnpAowWS5w3P6FsgGM1", "ePuUZ64mq5qhNcRL7UpcRUhUoZVeHUZl",baiduPanAccessTokenDTO.getRefresh_token());
// String refreshTokenRes = HttpUtil.get(refreshTokenUrl);
// System.out.println("refreshTokenRes="+refreshTokenRes);
// baiduPanAccessTokenDTO = JSONUtil.toBean(refreshTokenRes, BaiduPanAccessTokenDTO.class);
// System.out.println("refreshTokenRes bean="+baiduPanAccessTokenDTO);
return "index";
}
@RequestMapping("/login")
@ResponseBody
public void login(HttpServletResponse response) throws IOException {
String baseUrl = "http://openapi.baidu.com/oauth/2.0/authorize?response_type=code&client_id={0}&redirect_uri={1}&scope=basic,netdisk&qrcode=1";
String url = MessageFormat.format(baseUrl, "4DyoVFaBH2zoSAnpAowWS5w3P6FsgGM1", callback);
System.out.println("url="+url);
response.sendRedirect(url);
}
@RequestMapping("/imagesList")
@ResponseBody
public String imagesList(HttpServletResponse response) throws IOException, InterruptedException {
String url = "http://pan.baidu.com/rest/2.0/xpan/file";
BaiduPanImageListDTO baiduPanImageListDTO = new BaiduPanImageListDTO();
baiduPanImageListDTO.setAccess_token(accessToken.getAccess_token());
baiduPanImageListDTO.setParent_path(URLEncoder.encode("/电脑文件/手机图片/照片-杂", StandardCharsets.UTF_8));
baiduPanImageListDTO.setPage(1);
baiduPanImageListDTO.setNum(1);
baiduPanImageListDTO.setWeb(1);
Map<String, Object> stringObjectMap = BeanUtil.beanToMap(baiduPanImageListDTO);
String res = HttpUtil.get(url, stringObjectMap);
JSONObject json = JSONUtil.parseObj(res);
JSONArray info = json.getJSONArray("info");
JSONObject entries = info.get(0, JSONObject.class);
JSONObject thumbs = entries.get("thumbs", JSONObject.class);
String url3 = thumbs.getStr("url3");
Thread.sleep(1000 * 10);
res = HttpUtil.get(url, stringObjectMap);
json = JSONUtil.parseObj(res);
info = json.getJSONArray("info");
entries = info.get(0, JSONObject.class);
thumbs = entries.get("thumbs", JSONObject.class);
String url32 = thumbs.getStr("url3");
System.out.println(Objects.equals(url32, url3));
System.out.println("imagesList="+res);
return res;
}
@RequestMapping("/index")
public String index(HttpServletResponse response) throws IOException {
return "index";
}
}

View File

@ -0,0 +1,34 @@
package cn.jecyp.baidupanopenapi.pojo.dto;
import lombok.Data;
import java.io.Serializable;
/**
* 百度判断返回的accessToken对象
* @author cyp
* @date 2023-07-31
*/
@Data
public class BaiduPanAccessTokenDTO implements Serializable {
/**
* 获取到的Access Token是调用网盘开放API访问用户授权资源的凭证
*/
private String access_token;
/**
* Token的有效期单位为秒
*/
private String expires_in;
/**
* 用于刷新 Access Token, 有效期为10年
*/
private String refresh_token;
/**
* Access Token 最终的访问权限即用户的实际授权列表
*/
private String scope;
}

View File

@ -0,0 +1,56 @@
package cn.jecyp.baidupanopenapi.pojo.dto;
import lombok.Data;
/**
* @author cyp
* @date 2023-07-31
*/
@Data
public class BaiduPanImageListDTO {
/**
* URL参数 本接口固定为imagelist
*/
private String method = "imagelist";
/**
* URL参数 接口鉴权参数
*/
private String access_token;
/**
* URL参数 目录名称/开头的绝对路径, 默认为/ 路径包含中文时需要UrlEncode编码
*/
private String parent_path;
/**
* URL参数 页码从1开始 如果不指定页码则为不分页模式返回所有的结果 如果指定page参数则按修改时间倒序排列
*/
private Integer page;
/**
* URL参数 一页返回的图片数默认值为1000建议最大值不超过1000
*/
private Integer num;
/**
* URL参数 排序字段time按修改时间排序name按文件名称排序(注意此处排序是按字符串排序的如果用户有剧集排序需求需要自行开发)size按文件大小排序默认为time
*/
private String order;
/**
* URL参数 0为升序1为降序默认为1
*/
private Integer desc;
/**
* URL参数 是否需要递归0为不需要1为需要默认为0.递归是指当目录下有文件夹使用此参数可以获取到文件夹下面的图片
*/
private Integer recursion;
/**
* URL参数 为1时返回图片缩略图
*/
private Integer web;
}

View File

@ -0,0 +1,18 @@
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://rm-bp1r0y7o8554i172duo.mysql.rds.aliyuncs.com:3306/baidu_openapi?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8
username: aliyun_mysql
password: peng0012
server:
tomcat:
uri-encoding: UTF-8
port: 9000
servlet:
context-path: /openapi
session:
cookie:
http-only: true

View File

@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
hello
<a href="#" th:href="@{/baidu/pan/login}" class="ui mini teal basic button">登录</a>
<a href="#" th:href="@{/baidu/pan/imagesList}" class="ui mini teal basic button">获取图片</a>
</body>
</html>

View File

@ -0,0 +1,13 @@
package cn.jecyp.baidupanopenapi;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class BaiduPanOpenapiApplicationTests {
@Test
void contextLoads() {
}
}