@@ -24,20 +24,23 @@ public class AliYunDriverClient {
24
24
private Runnable onRefreshTokenInvalidListener ;
25
25
26
26
public Request buildCommonRequestHeader (Request request ) {
27
- return request .newBuilder ()
27
+ Request . Builder builder = request .newBuilder ()
28
28
.removeHeader ("User-Agent" )
29
29
.addHeader ("User-Agent" , aliYunDriveProperties .agent )
30
- .removeHeader ("authorization" )
31
- .addHeader ("authorization" , "Bearer\t " + aliYunDriveProperties .authorization )
32
30
.removeHeader ("x-device-id" )
33
31
.addHeader ("x-device-id" , aliYunDriveProperties .deviceId )
34
32
.removeHeader ("x-signature" )
35
33
.addHeader ("x-signature" , aliYunDriveProperties .session .signature + "01" )
36
34
.removeHeader ("x-canary" )
37
35
.addHeader ("x-canary" , "client=web,app=adrive,version=v3.17.0" )
38
36
.removeHeader ("x-request-id" )
39
- .addHeader ("x-request-id" , UUID .randomUUID ().toString ())
40
- .build ();
37
+ .addHeader ("x-request-id" , UUID .randomUUID ().toString ());
38
+
39
+ builder .removeHeader ("authorization" );
40
+ if (!StringUtils .isEmpty (aliYunDriveProperties .authorization )) {
41
+ builder .addHeader ("authorization" , "Bearer\t " + aliYunDriveProperties .authorization );
42
+ }
43
+ return builder .build ();
41
44
}
42
45
43
46
public AliYunDriverClient (AliYunDriveProperties aliYunDriveProperties ) {
@@ -61,6 +64,20 @@ public Response intercept(Chain chain) throws IOException {
61
64
AliYunDriverClient .this .aliYunDriveProperties .session .expireTimeSec = 0 ;
62
65
AliYunDriverClient .this .aliYunDriveProperties .save ();
63
66
LOGGER .error ("登录设备过多, 请进入\" 登录设备管理\" , 退出一些设备。" );
67
+ if (!url .endsWith ("/token/refresh" )) {
68
+ requestAuthorization ();
69
+ Response retryResponse = chain .proceed (buildCommonRequestHeader (request ));
70
+ ResponseBody retryBody = response .peekBody (40960 );
71
+ String retryRes = retryBody .string ();
72
+ if (retryRes .contains ("UserDeviceOffline" )) {
73
+ LOGGER .error ("重新登录失败, 设备数过多, 等待30分钟..." );
74
+ //防止请求数过多
75
+ try {
76
+ TimeUnit .MINUTES .sleep (30 );
77
+ } catch (InterruptedException e ) {
78
+ }
79
+ }
80
+ }
64
81
return response ;
65
82
}
66
83
}
@@ -74,28 +91,7 @@ public Request authenticate(Route route, Response response) throws IOException {
74
91
ResponseBody body = response .peekBody (40960 );
75
92
String res = body .string ();
76
93
if (res .contains ("AccessToken" )) {
77
- String refreshTokenResult ;
78
- try {
79
- if (StringUtils .isEmpty (aliYunDriveProperties .refreshToken )) {
80
- throw new NullPointerException ();
81
- }
82
- refreshTokenResult = post ("https://api.aliyundrive.com/token/refresh" , Collections .singletonMap ("refresh_token" , aliYunDriveProperties .refreshToken ));
83
- } catch (Exception e ) {
84
- refreshTokenResult = post ("https://api.aliyundrive.com/token/refresh" , Collections .singletonMap ("refresh_token" , aliYunDriveProperties .refreshTokenNext ));
85
- }
86
- String accessToken = (String ) JsonUtil .getJsonNodeValue (refreshTokenResult , "access_token" );
87
- String refreshToken = (String ) JsonUtil .getJsonNodeValue (refreshTokenResult , "refresh_token" );
88
- String userId = (String ) JsonUtil .getJsonNodeValue (refreshTokenResult , "user_id" );
89
- if (StringUtils .isEmpty (accessToken ))
90
- throw new IllegalArgumentException ("获取accessToken失败" );
91
- if (StringUtils .isEmpty (refreshToken ))
92
- throw new IllegalArgumentException ("获取refreshToken失败" );
93
- if (StringUtils .isEmpty (refreshToken ))
94
- throw new IllegalArgumentException ("获取userId失败" );
95
- aliYunDriveProperties .userId = userId ;
96
- aliYunDriveProperties .authorization = accessToken ;
97
- aliYunDriveProperties .refreshToken = refreshToken ;
98
- aliYunDriveProperties .save ();
94
+ String accessToken = requestAuthorization ();
99
95
return response .request ().newBuilder ()
100
96
.removeHeader ("authorization" )
101
97
.header ("authorization" , accessToken )
@@ -116,6 +112,33 @@ public Request authenticate(Route route, Response response) throws IOException {
116
112
}
117
113
}
118
114
115
+ private String requestAuthorization () {
116
+ String refreshTokenResult ;
117
+ aliYunDriveProperties .authorization = null ;
118
+ try {
119
+ if (StringUtils .isEmpty (aliYunDriveProperties .refreshToken )) {
120
+ throw new NullPointerException ();
121
+ }
122
+ refreshTokenResult = post ("https://api.aliyundrive.com/token/refresh" , Collections .singletonMap ("refresh_token" , aliYunDriveProperties .refreshToken ));
123
+ } catch (Exception e ) {
124
+ refreshTokenResult = post ("https://api.aliyundrive.com/token/refresh" , Collections .singletonMap ("refresh_token" , aliYunDriveProperties .refreshTokenNext ));
125
+ }
126
+ String accessToken = (String ) JsonUtil .getJsonNodeValue (refreshTokenResult , "access_token" );
127
+ String refreshToken = (String ) JsonUtil .getJsonNodeValue (refreshTokenResult , "refresh_token" );
128
+ String userId = (String ) JsonUtil .getJsonNodeValue (refreshTokenResult , "user_id" );
129
+ if (StringUtils .isEmpty (accessToken ))
130
+ throw new IllegalArgumentException ("获取accessToken失败" );
131
+ if (StringUtils .isEmpty (refreshToken ))
132
+ throw new IllegalArgumentException ("获取refreshToken失败" );
133
+ if (StringUtils .isEmpty (refreshToken ))
134
+ throw new IllegalArgumentException ("获取userId失败" );
135
+ aliYunDriveProperties .userId = userId ;
136
+ aliYunDriveProperties .authorization = accessToken ;
137
+ aliYunDriveProperties .refreshToken = refreshToken ;
138
+ aliYunDriveProperties .save ();
139
+ return accessToken ;
140
+ }
141
+
119
142
private void login () {
120
143
// todo 暂不支持登录功能
121
144
}
0 commit comments