BilibiliAPI 现在可以自定义是否自动刷新 Token

This commit is contained in:
czp 2018-03-07 09:31:44 +08:00
parent 4d2ae98561
commit 72ea245c7f
5 changed files with 136 additions and 23 deletions

View File

@ -40,6 +40,8 @@ public class BilibiliAPI implements BilibiliServiceProvider, BilibiliCaptchaProv
private final BilibiliClientProperties bilibiliClientProperties; private final BilibiliClientProperties bilibiliClientProperties;
private final BilibiliAccount bilibiliAccount; private final BilibiliAccount bilibiliAccount;
private Boolean autoRefreshToken = true;
//用于阻止进行多次错误的 refreshToken 操作 //用于阻止进行多次错误的 refreshToken 操作
private String invalidToken; private String invalidToken;
private String invalidRefreshToken; private String invalidRefreshToken;
@ -142,8 +144,9 @@ public class BilibiliAPI implements BilibiliServiceProvider, BilibiliCaptchaProv
() -> "trace_id", () -> new SimpleDateFormat("yyyyMMddHHmm000ss").format(new Date()) () -> "trace_id", () -> new SimpleDateFormat("yyyyMMddHHmm000ss").format(new Date())
)) ))
.addInterceptor(new AddAppKeyInterceptor(bilibiliClientProperties)) .addInterceptor(new AddAppKeyInterceptor(bilibiliClientProperties))
.addInterceptor(new RefreshTokenInterceptor( .addInterceptor(new AutoRefreshTokenInterceptor(
this, this,
autoRefreshToken,
ServerErrorCode.Common.UNAUTHORIZED, ServerErrorCode.Common.UNAUTHORIZED,
ServerErrorCode.Live.USER_NO_LOGIN, ServerErrorCode.Live.USER_NO_LOGIN,
ServerErrorCode.Live.PLEASE_LOGIN, ServerErrorCode.Live.PLEASE_LOGIN,
@ -401,4 +404,13 @@ public class BilibiliAPI implements BilibiliServiceProvider, BilibiliCaptchaProv
public BilibiliAccount getBilibiliAccount() { public BilibiliAccount getBilibiliAccount() {
return bilibiliAccount; return bilibiliAccount;
} }
public boolean isAutoRefreshToken() {
return autoRefreshToken;
}
public BilibiliAPI setAutoRefreshToken(boolean autoRefreshToken) {
this.autoRefreshToken = autoRefreshToken;
return this;
}
} }

View File

@ -13,14 +13,16 @@ import java.io.IOException;
import java.util.stream.IntStream; import java.util.stream.IntStream;
//自动刷新 token //自动刷新 token
public class RefreshTokenInterceptor implements Interceptor { public class AutoRefreshTokenInterceptor implements Interceptor {
private static final Logger LOGGER = LoggerFactory.getLogger(RefreshTokenInterceptor.class); private static final Logger LOGGER = LoggerFactory.getLogger(AutoRefreshTokenInterceptor.class);
private BilibiliAPI bilibiliAPI; private BilibiliAPI bilibiliAPI;
private Boolean autoRefreshToken;
private int[] codes; private int[] codes;
public RefreshTokenInterceptor(BilibiliAPI bilibiliAPI, int... codes) { public AutoRefreshTokenInterceptor(BilibiliAPI bilibiliAPI, Boolean autoRefreshToken, int... codes) {
this.bilibiliAPI = bilibiliAPI; this.bilibiliAPI = bilibiliAPI;
this.autoRefreshToken = autoRefreshToken;
this.codes = codes; this.codes = codes;
} }
@ -28,6 +30,10 @@ public class RefreshTokenInterceptor implements Interceptor {
public Response intercept(Chain chain) throws IOException { public Response intercept(Chain chain) throws IOException {
Response response = chain.proceed(chain.request()); Response response = chain.proceed(chain.request());
if (!autoRefreshToken) {
return response;
}
JsonObject jsonObject = InterceptorHelper.getJsonInBody(response); JsonObject jsonObject = InterceptorHelper.getJsonInBody(response);
JsonElement codeElement = jsonObject.get("code"); JsonElement codeElement = jsonObject.get("code");
if (codeElement == null) { if (codeElement == null) {

View File

@ -32,7 +32,7 @@ public interface LiveService {
@POST("feed/v1/feed/isFollowed") @POST("feed/v1/feed/isFollowed")
Call<IsFollowedResponseEntity> isFollowed(@Query("follow") long hostUserId); Call<IsFollowedResponseEntity> isFollowed(@Query("follow") long hostUserId);
// API 意义不明 // API 意义不明(似乎跟什么 每日背包任务 有关)
@GET("AppBag/sendDaily") @GET("AppBag/sendDaily")
Call<SendDailyResponseEntity> sendDaily(); Call<SendDailyResponseEntity> sendDaily();
@ -40,16 +40,19 @@ public interface LiveService {
@GET("AppIndex/getAllItem") @GET("AppIndex/getAllItem")
Call<ItemsEntity> getAllItem(); Call<ItemsEntity> getAllItem();
// API 的返回值意义不明确, 所有房间似乎都一样, 且返回的 code -400 //查看可用的小电视抽奖
//当目标房间没有可用的小电视抽奖时返回 -400
@GET("AppSmallTV/index") @GET("AppSmallTV/index")
Call<AppSmallTVEntity> getAppSmallTV(); Call<AppSmallTVEntity> getAppSmallTV(@Query("roomid") long roomId);
//TODO 参与抽奖
//TODO 查看抽奖结果
//获得所有头衔的列表 //获得所有头衔的列表
//这里的 Title 是头衔的意思 //这里的 Title 是头衔的意思
@GET("appUser/getTitle") @GET("appUser/getTitle")
Call<TitlesEntity> getTitle(); Call<TitlesEntity> getTitle();
//这个 API 不是很明确, 所有房间都一样, 可能和什么活动有关 //这个 API 不是很明确, 似乎和 节奏风暴 有关系
@GET("SpecialGift/room/{roomId}") @GET("SpecialGift/room/{roomId}")
Call<SpecialGiftEntity> getSpecialGift(@Path("roomId") long roomId); Call<SpecialGiftEntity> getSpecialGift(@Path("roomId") long roomId);

View File

@ -2,19 +2,24 @@ package com.hiczp.bilibili.api.live.entity;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import java.util.List;
public class AppSmallTVEntity { public class AppSmallTVEntity {
/** /**
* code : -400 * code : 0
* message : no * msg : OK
* data : {"status":-1} * message : OK
* data : {"lastid":0,"join":[{"id":39674,"dtime":32}],"unjoin":[{"id":39674,"dtime":32}]}
*/ */
@SerializedName("code") @SerializedName("code")
private int code; private int code;
@SerializedName("msg")
private String msg;
@SerializedName("message") @SerializedName("message")
private String message; private String message;
@SerializedName("data") @SerializedName("data")
private DataEntity data; private Data data;
public int getCode() { public int getCode() {
return code; return code;
@ -24,6 +29,14 @@ public class AppSmallTVEntity {
this.code = code; this.code = code;
} }
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getMessage() { public String getMessage() {
return message; return message;
} }
@ -32,28 +45,106 @@ public class AppSmallTVEntity {
this.message = message; this.message = message;
} }
public DataEntity getData() { public Data getData() {
return data; return data;
} }
public void setData(DataEntity data) { public void setData(Data data) {
this.data = data; this.data = data;
} }
public static class DataEntity { public static class Data {
/** /**
* status : -1 * lastid : 0
* join : [{"id":39674,"dtime":32}]
* unjoin : [{"id":39674,"dtime":32}]
*/ */
@SerializedName("status") @SerializedName("lastid")
private int status; private int lastid;
@SerializedName("join")
private List<Join> join;
@SerializedName("unjoin")
private List<Unjoin> unjoin;
public int getStatus() { public int getLastid() {
return status; return lastid;
} }
public void setStatus(int status) { public void setLastid(int lastid) {
this.status = status; this.lastid = lastid;
}
public List<Join> getJoin() {
return join;
}
public void setJoin(List<Join> join) {
this.join = join;
}
public List<Unjoin> getUnjoin() {
return unjoin;
}
public void setUnjoin(List<Unjoin> unjoin) {
this.unjoin = unjoin;
}
public static class Join {
/**
* id : 39674
* dtime : 32
*/
@SerializedName("id")
private int id;
@SerializedName("dtime")
private int dtime;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getDtime() {
return dtime;
}
public void setDtime(int dtime) {
this.dtime = dtime;
}
}
public static class Unjoin {
/**
* id : 39674
* dtime : 32
*/
@SerializedName("id")
private int id;
@SerializedName("dtime")
private int dtime;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getDtime() {
return dtime;
}
public void setDtime(int dtime) {
this.dtime = dtime;
}
} }
} }
} }

View File

@ -7,7 +7,8 @@ public class BulletScreenEntity {
private long mid; private long mid;
//弹幕长度限制为 LiveRoomInfoEntity.getData().getMsgLength(), 但是实际上所有房间的弹幕长度限制都是 20 //弹幕长度限制为 LiveRoomInfoEntity.getData().getMsgLength(), 对于每个用户而言, 每个房间都一样
//通过完成 B站 有关任务, 获得成就, 可以加大这个限制(舰长, 老爷等可以直接加大限制), 最长好像是 40 个字
@SerializedName("msg") @SerializedName("msg")
private String message; private String message;