Update ali api flow - part 1

This commit is contained in:
FongMi 2023-05-11 01:26:17 +08:00
parent 39f0cc50d8
commit 5d4d95d7de
1 changed files with 19 additions and 21 deletions

View File

@ -103,7 +103,6 @@ public class API {
public void setShareId(String shareId) { public void setShareId(String shareId) {
this.shareId = shareId; this.shareId = shareId;
refreshShareToken(); refreshShareToken();
checkAccessToken();
} }
public HashMap<String, String> getHeader() { public HashMap<String, String> getHeader() {
@ -130,6 +129,7 @@ public class API {
url = "https://api.nn.ci/alist/ali_open/" + url; url = "https://api.nn.ci/alist/ali_open/" + url;
OkResult result = OkHttp.postJson(url, body.toString(), getHeader()); OkResult result = OkHttp.postJson(url, body.toString(), getHeader());
if (isManyRequest(result.getBody())) return ""; if (isManyRequest(result.getBody())) return "";
SpiderDebug.log(result.getCode() + "," + url + "," + result.getBody());
if (result.getCode() == 200) return result.getBody(); if (result.getCode() == 200) return result.getBody();
throw new Exception(result.getBody()); throw new Exception(result.getBody());
} }
@ -142,14 +142,17 @@ public class API {
private String auth(String url, String json, boolean retry) { private String auth(String url, String json, boolean retry) {
url = url.startsWith("https") ? url : "https://api.aliyundrive.com/" + url; url = url.startsWith("https") ? url : "https://api.aliyundrive.com/" + url;
OkResult result = OkHttp.postJson(url, json, getHeaderAuth()); OkResult result = OkHttp.postJson(url, json, getHeaderAuth());
if (retry && result.getCode() != 200 && refreshAccessToken()) return auth(url, json, false); SpiderDebug.log(result.getCode() + "," + url + "," + result.getBody());
if (retry && result.getCode() == 401 && refreshAccessToken()) return auth(url, json, false);
if (retry && result.getCode() == 429) return auth(url, json, false);
return result.getBody(); return result.getBody();
} }
private String oauth(String url, String json, boolean retry) { private String oauth(String url, String json, boolean retry) {
url = url.startsWith("https") ? url : "https://open.aliyundrive.com/adrive/v1.0/" + url; url = url.startsWith("https") ? url : "https://open.aliyundrive.com/adrive/v1.0/" + url;
OkResult result = OkHttp.postJson(url, json, getHeaderOpen()); OkResult result = OkHttp.postJson(url, json, getHeaderOpen());
if (retry && result.getCode() != 200 && refreshOpenToken()) return oauth(url, json, false); SpiderDebug.log(result.getCode() + "," + url + "," + result.getBody());
if (retry && result.getCode() == 401 && refreshOpenToken()) return oauth(url, json, false);
return result.getBody(); return result.getBody();
} }
@ -166,8 +169,18 @@ public class API {
return false; return false;
} }
public void checkAccessToken() { private void refreshShareToken() {
if (user.getAccessToken().isEmpty()) refreshAccessToken(); try {
SpiderDebug.log("refreshShareToken...");
JSONObject body = new JSONObject();
body.put("share_id", shareId);
body.put("share_pwd", "");
String result = post("v2/share_link/get_share_token", body);
shareToken = new JSONObject(result).getString("share_token");
} catch (Exception e) {
e.printStackTrace();
Init.show("來晚啦,該分享已失效。");
}
} }
private boolean refreshAccessToken() { private boolean refreshAccessToken() {
@ -183,7 +196,6 @@ public class API {
user = User.objectFrom(result).save(); user = User.objectFrom(result).save();
SpiderDebug.log(user.toString()); SpiderDebug.log(user.toString());
if (user.getAccessToken().isEmpty()) throw new Exception(result); if (user.getAccessToken().isEmpty()) throw new Exception(result);
if (oauth.getAccessToken().isEmpty()) oauthRequest();
return true; return true;
} catch (Exception e) { } catch (Exception e) {
if (e instanceof TimeoutException) return onTimeout(); if (e instanceof TimeoutException) return onTimeout();
@ -235,25 +247,10 @@ public class API {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
oauth.clean().save(); oauth.clean().save();
oauthRequest();
return true; return true;
} }
} }
private void refreshShareToken() {
try {
SpiderDebug.log("refreshShareToken...");
JSONObject body = new JSONObject();
body.put("share_id", shareId);
body.put("share_pwd", "");
String result = post("v2/share_link/get_share_token", body);
shareToken = new JSONObject(result).getString("share_token");
} catch (Exception e) {
e.printStackTrace();
Init.show("來晚啦,該分享已失效。");
}
}
public Vod getVod(String url, String fileId) throws Exception { public Vod getVod(String url, String fileId) throws Exception {
JSONObject body = new JSONObject(); JSONObject body = new JSONObject();
body.put("share_id", shareId); body.put("share_id", shareId);
@ -404,6 +401,7 @@ public class API {
String json = "{\"requests\":[{\"body\":{\"file_id\":\"%s\",\"share_id\":\"%s\",\"auto_rename\":true,\"to_parent_file_id\":\"root\",\"to_drive_id\":\"%s\"},\"headers\":{\"Content-Type\":\"application/json\"},\"id\":\"0\",\"method\":\"POST\",\"url\":\"/file/copy\"}],\"resource\":\"file\"}"; String json = "{\"requests\":[{\"body\":{\"file_id\":\"%s\",\"share_id\":\"%s\",\"auto_rename\":true,\"to_parent_file_id\":\"root\",\"to_drive_id\":\"%s\"},\"headers\":{\"Content-Type\":\"application/json\"},\"id\":\"0\",\"method\":\"POST\",\"url\":\"/file/copy\"}],\"resource\":\"file\"}";
json = String.format(json, fileId, shareId, user.getDriveId()); json = String.format(json, fileId, shareId, user.getDriveId());
String result = auth("adrive/v2/batch", json, true); String result = auth("adrive/v2/batch", json, true);
if (result.contains("ForbiddenNoPermission.File")) return copy(fileId);
return new JSONObject(result).getJSONArray("responses").getJSONObject(0).getJSONObject("body").getString("file_id"); return new JSONObject(result).getJSONArray("responses").getJSONObject(0).getJSONObject("body").getString("file_id");
} }