百度网盘开放接口
实现百度网盘的文件查询,创建分享链接(要钱,贵的雅痞,个人价格劝退)
(分享能力均为年度收费:标准方案:19999元/年(包含:调用分享所有接口(分享、解析、转存)))
**
* 百度网盘对接
*/
@Slf4j
@Service
@RequiredArgsConstructor
public class BaiduNetDiskService {
private final JedisClusterBSJ jedisCluster;
private final RestTemplate restTemplate;
private final static String AppID = "43371";
private final static String AppKey = "alu2uAjyQCnyXBSkWqGcZGynZW";
private final static String Secretkey = "Fv3NqNMO1FepwW5bBa6p07N3r";
private final static String Signkey = "VTgK@sUiYmEXiiHHOuezl%*LG@c";
private final static String AccessToken = "BaiduNetDisk:43371:AccessToken";
private final static String RefreshToken = "BaiduNetDisk:43361:RefreshToken";
private final static String codeUrl = "http://openapi.baidu.com/oauth/2.0/authorize?response_type=code&client_id=alu2uAn7hAwynZW&redirect_uri=oob&scope=basic,netdisk";
private final static String accessTokenUrl = "https://openapi.baidu.com/oauth/2.0/token?grant_type=authorization_code&client_id=alu2uQcZGso7hAwynZW&client_secret=Fv3NqNMOCpgHvBa6p07N3r&redirect_uri=oob&code=";
@PostConstruct
public void test() {
createShareFile();
}
public void createShareFile() {
String url = "https://pan.baidu.com/apaas/1.0/share/set?product=netdisk&appid=43370961&access_token=";
url = url + getAccessToken();
List<String> list = new ArrayList<>();
list.add("426852352184438");
JSONObject jsonObject = new JSONObject();
jsonObject.put("fsid_list", JSONObject.toJSONString(list));
jsonObject.put("period", 3);
jsonObject.put("pwd", "12sf");
String rsp = restTemplate.postForObject(url, jsonObject, String.class);
log.info("获取到当前的创建文件信息:{}", rsp);
}
public void getFileId(String fileName) {
String url = "https://pan.baidu.com/rest/2.0/xpan/file?method=list&access_token=" + getAccessToken();
if (StringUtil.isNotBlank(fileName)) {
url = url + "&dir=/暮看云/" + fileName;
}
String rsp = restTemplate.getForObject(urlEncodeChinese(url), String.class);
log.info("获取到当前的refresh_token:{}", rsp);
JSONObject json = JSONObject.parseObject(rsp);
JSONArray list = json.getJSONArray("list");
log.info("获取到当前的list:{}", JSONObject.toJSONString(list));
}
private String getAccessToken() {
String data = jedisCluster.get(AccessToken);
if (data != null) {
return data;
}
String accessToken = null;
String refreshToken = jedisCluster.get(RefreshToken);
if (refreshToken != null) {
String refreshUrl = "https://openapi.baidu.com/oauth/2.0/token?grant_type=refresh_token&client_id=alu2uAjyQkWqGcZGso7hAwynZW&client_secret=Fv3NqNMO1FepwWBa6p07N3r&refresh_token=";
refreshUrl = refreshUrl + refreshToken;
String rsp = restTemplate.getForObject(refreshUrl, String.class);
log.info("获取到当前的refresh_token:{}", rsp);
JSONObject json = JSONObject.parseObject(rsp);
Integer expiresIn = json.getInteger("expires_in");
String access_token = json.getString("access_token");
jedisCluster.setex(AccessToken, (expiresIn - 5), access_token);
String refresh_token = json.getString("refresh_token");
jedisCluster.setex(RefreshToken, expiresIn * 12 * 10, refresh_token);
return access_token;
} else {
jedisCluster.setex(RefreshToken, 2592000 * 12 * 10, "122.24ffe5250c66b3a5c496a3c40.YDyYOunB6Yw5O9Z5cMRqmJGLnbg0-.MMU-Og");
accessToken = getAccessToken();
}
if (accessToken != null) {
return accessToken;
}
String sendUrl = accessTokenUrl + getCode();
String rsp = restTemplate.getForObject(sendUrl, String.class);
log.info("获取到当前的getAccessToken:{}", rsp);
JSONObject json = JSONObject.parseObject(rsp);
Integer expiresIn = json.getInteger("expires_in");
String access_token = json.getString("access_token");
jedisCluster.setex(AccessToken, (expiresIn - 5), access_token);
String refresh_token = json.getString("refresh_token");
jedisCluster.setex(RefreshToken, expiresIn * 12, refresh_token);
return access_token;
}
private String getCode() {
return "084580d3509537d18abc0f29daac4f74";
}
private static String urlEncodeChinese(String url) {
try {
Matcher matcher = Pattern.compile("[\\u4e00-\\u9fa5]").matcher(url);
String tmp = "";
while (matcher.find()) {
tmp = matcher.group();
url = url.replaceAll(tmp, URLEncoder.encode(tmp, "UTF-8"));
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return url.replace(" ", "%20");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135