From 012660dd7b1632b5c02d7b471d623b5823421413 Mon Sep 17 00:00:00 2001 From: czp3009 Date: Tue, 19 Feb 2019 23:00:25 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E6=8B=A6=E6=88=AA=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/hiczp/bilibili/api/BilibiliClient.kt | 24 +++++++++---------- .../bilibili/api/passport/PassportAPI.kt | 8 +++---- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/main/kotlin/com/hiczp/bilibili/api/BilibiliClient.kt b/src/main/kotlin/com/hiczp/bilibili/api/BilibiliClient.kt index d837075..de31d93 100644 --- a/src/main/kotlin/com/hiczp/bilibili/api/BilibiliClient.kt +++ b/src/main/kotlin/com/hiczp/bilibili/api/BilibiliClient.kt @@ -34,7 +34,7 @@ private val logger = KotlinLogging.logger {} class BilibiliClient( @Suppress("MemberVisibilityCanBePrivate") val billingClientProperties: BilibiliClientProperties = BilibiliClientProperties(), - private val autoRefreshToken: Boolean = true, + private val autoRefreshToken: Boolean = true, //TODO 自动 refreshToken private val logLevel: HttpLoggingInterceptor.Level = HttpLoggingInterceptor.Level.NONE ) { /** @@ -45,6 +45,7 @@ class BilibiliClient( /** * 登陆操作得到的 Response */ + @Suppress("MemberVisibilityCanBePrivate") var loginResponse: LoginResponse? = null /** @@ -58,13 +59,7 @@ class BilibiliClient( val passportAPI: PassportAPI by lazy { val okHttpClient = OkHttpClient.Builder().apply { addInterceptor(CommonHeaderInterceptor( - "Display-ID" to { - if (isLogin) { - "${billingClientProperties.buildVersionId}-$initTime" - } else { - billingClientProperties.buildVersionId - } - }, + "Display-ID" to { "${billingClientProperties.buildVersionId}-$initTime" }, "Buvid" to { billingClientProperties.buildVersionId }, "User-Agent" to { "Mozilla/5.0 BiliDroid/5.37.0 (bbcallen@gmail.com)" }, "Device-ID" to { billingClientProperties.hardwareId } @@ -95,15 +90,20 @@ class BilibiliClient( .create(PassportAPI::class.java) } - //TODO 验证码 /** * 登陆 * v3 登陆接口会同时返回 cookies 和 token * - * @throws BilibiliApiException 用户名与密码不匹配或者需要验证码 + * @throws BilibiliApiException 用户名与密码不匹配(-629)或者需要验证码(极验)(-105) */ @Throws(BilibiliApiException::class) - suspend fun login(username: String, password: String): LoginResponse { + suspend fun login( + username: String, password: String, + //如果登陆请求返回了 "验证码错误!"(-105) 的结果, 那么下一次发送登陆请求就需要带上验证码 + challenge: String? = null, + secCode: String? = null, + validate: String? = null + ): LoginResponse { //取得 hash 和 RSA 公钥 val (hash, key) = passportAPI.getKey().await().data.let { data -> data.hash to data.key.split('\n').filterNot { it.startsWith('-') }.joinToString(separator = "") @@ -123,7 +123,7 @@ class BilibiliClient( String(it) } - return passportAPI.login(username, cipheredPassword).await().also { + return passportAPI.login(username, cipheredPassword, challenge, secCode, validate).await().also { this.loginResponse = it } } diff --git a/src/main/kotlin/com/hiczp/bilibili/api/passport/PassportAPI.kt b/src/main/kotlin/com/hiczp/bilibili/api/passport/PassportAPI.kt index b0a32b2..f50d191 100644 --- a/src/main/kotlin/com/hiczp/bilibili/api/passport/PassportAPI.kt +++ b/src/main/kotlin/com/hiczp/bilibili/api/passport/PassportAPI.kt @@ -22,10 +22,10 @@ interface PassportAPI { @FormUrlEncoded fun login( @Field("username") username: String, @Field("password") password: String, - //以下为验证码所需字段 - @Field("challenge") challenge: String? = null, //系统给出的滑动验证 ID - @Field("seccode") secCode: String? = null, //用户给出的滑动验证答案, 生成算法不明 - @Field("validate") validate: String? = null //滑动验证校验字段, 算法不明 + //以下为极验所需字段 + @Field("challenge") challenge: String? = null, + @Field("seccode") secCode: String? = null, + @Field("validate") validate: String? = null ): Deferred /**