czp3009 / bilibili-api

Bilibili API library for Kotlin
GNU General Public License v3.0
513 stars 50 forks source link

在android上运行返回-400 Bad request #2

Closed SakuraKoi closed 6 years ago

SakuraKoi commented 6 years ago

在使用您的api开发的过程中碰到一点问题,因此请教一下

首先,为了能在android上运行,我修改了部分代码(将lambda改为匿名类,以及自行实现部分java8新增的api),我可以确认这部分代码不会引起问题

我遇到的问题是 在我的电脑(ubuntu 14.04.5)上,经测试一切正常 但在安卓上同样的代码登录时返回了code:-400, message:"请求出错,请重试"

经过抓包对比,我并没能找到问题....看起来两个post请求没什么不对

请问这种情况有可能是什么造成的?

czp3009 commented 6 years ago

Android 上编译 Java8 代码推荐用 Jack,因为比如用 lambda 创建一个 Supplier 以及 Stream 操作好像在 Java7 是做不出来的。

我的这个包已经发布到了 Maven中心站,可以直接从 Gradle 引入。

Retrofit 在 Android 上好像不太一样(我写 Android 比较少,不是很清楚),Guava 可以确定在 Android 上是要用另一个 Android 专版的。这个包设计的时候没有考虑在 Android 使用的问题,本来是拿来重构我的弹幕机和B站云签到平台。

Android 上我没有调试过,我想看一下控制台输出(retrofit 会打印出请求的 URL)以及登录操作返回的 LoginResponseEntity 里面的内容(可以在 Idea 上看)(用 BilibiliSecurityHelper 这个类,它不判断返回值)。

代码差不多是这样的

LoginResponseEntity loginResponseEntity = BilibiliSecurityHelper.login(
        new BilibiliAPI(),
        username,
        password
    );

另外有一个事情我在文档里没有提到,B站晚上(不明确具体时间表,可能是十二点到两点)会维护服务器,此时所有请求都会报错(body里面的code不是0)(HttpStatus依然是200),这是一种错误的原因,但是大概不是你的问题的答案,只是说一下而已。

SakuraKoi commented 6 years ago

这是在android上的控制台输出

--> POST https://passport.bilibili.com/api/oauth2/getKey?appkey=1d8b6e7d45233436&build=515000&mobi_app=android&platform=android&ts=1517640940353&sign=7d7eddf654bf1fa58588cc2ddec2f0d7 http/1.1
Content-Length: 0
Host: passport.bilibili.com
Connection: Keep-Alive
Accept-Encoding: gzip
User-Agent: okhttp/3.8.0
--> END POST (0-byte body)
<-- 200 OK https://passport.bilibili.com/api/oauth2/getKey?appkey=1d8b6e7d45233436&build=515000&mobi_app=android&platform=android&ts=1517640940353&sign=7d7eddf654bf1fa58588cc2ddec2f0d7 (153ms)
Date: Sat, 03 Feb 2018 06:55:42 GMT
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Server: Apache-Coyote/1.1
Set-Cookie: sid=bwjan00d; Domain=.bilibili.com; Expires=Sun, 03-Feb-2019 06:55:42 GMT; Path=/
X-Upstream: 172.18.17.73:80
X-TKID: 1517640942715399886160
Expires: Sat, 03 Feb 2018 06:55:41 GMT
Cache-Control: no-cache
X-Cache: BYPASS from ks-bj6-webcdn-02.hdslb.com
Content-Encoding: gzip
<-- END HTTP (encoded body omitted)
--> POST https://passport.bilibili.com/api/oauth2/login?appkey=1d8b6e7d45233436&build=515000&mobi_app=android&password=AdAUwC4cUKD6AKFCLrMCzP421FG8mtsq345gGNeeUT%2BVG7znnjdXjE9GY%2F2DDjL6YrIOYifvoqH7nUYkZVwkRyveHbNxHvri7wbZCZA64YtKbhK9n2L0l4TzXKxtnFm2uCDXDSiGx9WHSKE1f9TSiL90iiQAKCNv3F9nhPqv3vw%3D&platform=android&ts=1517640942532&username=test&sign=a03dc6b939f952de2488117dee9aaad2 http/1.1
Content-Length: 0
Host: passport.bilibili.com
Connection: Keep-Alive
Accept-Encoding: gzip
User-Agent: okhttp/3.8.0
--> END POST (0-byte body)
<-- 200 OK https://passport.bilibili.com/api/oauth2/login?appkey=1d8b6e7d45233436&build=515000&mobi_app=android&password=AdAUwC4cUKD6AKFCLrMCzP421FG8mtsq345gGNeeUT%2BVG7znnjdXjE9GY%2F2DDjL6YrIOYifvoqH7nUYkZVwkRyveHbNxHvri7wbZCZA64YtKbhK9n2L0l4TzXKxtnFm2uCDXDSiGx9WHSKE1f9TSiL90iiQAKCNv3F9nhPqv3vw%3D&platform=android&ts=1517640942532&username=test&sign=a03dc6b939f952de2488117dee9aaad2 (101ms)
Date: Sat, 03 Feb 2018 06:55:43 GMT
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Server: Apache-Coyote/1.1
Set-Cookie: sid=cjufruoj; Domain=.bilibili.com; Expires=Sun, 03-Feb-2019 06:55:43 GMT; Path=/
X-Upstream: 172.18.17.21:80
X-TKID: 1517640943846137945130
Expires: Sat, 03 Feb 2018 06:55:42 GMT
Cache-Control: no-cache
X-Cache: BYPASS from ks-bj6-webcdn-02.hdslb.com
Content-Encoding: gzip
<-- END HTTP (encoded body omitted)

这是电脑上的输出

--> POST https://passport.bilibili.com/api/oauth2/getKey?appkey=1d8b6e7d45233436&build=515000&mobi_app=android&platform=android&ts=1517641251964&sign=8a72925768fb44b09fa43900afb98797 http/1.1
Content-Length: 0
Host: passport.bilibili.com
Connection: Keep-Alive
Accept-Encoding: gzip
User-Agent: okhttp/3.8.0
--> END POST (0-byte body)
<-- 200 OK https://passport.bilibili.com/api/oauth2/getKey?appkey=1d8b6e7d45233436&build=515000&mobi_app=android&platform=android&ts=1517641251964&sign=8a72925768fb44b09fa43900afb98797 (137ms)
Date: Sat, 03 Feb 2018 07:00:53 GMT
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Server: Apache-Coyote/1.1
Set-Cookie: sid=keub0qaw; Domain=.bilibili.com; Expires=Sun, 03-Feb-2019 07:00:53 GMT; Path=/
X-Upstream: 172.18.17.73:80
X-TKID: 1517641253414451704437
Expires: Sat, 03 Feb 2018 07:00:52 GMT
Cache-Control: no-cache
X-Cache: BYPASS from cn-jlcc-cu-w-01.hdslb.com
Content-Encoding: gzip
<-- END HTTP (encoded body omitted)
--> POST https://passport.bilibili.com/api/oauth2/login?appkey=1d8b6e7d45233436&build=515000&mobi_app=android&password=a420%2Bl1tgoSbdLNA7GhrzXexWtp19LSf5ixbX8mmA7DIVr2Qg8bd8yEsO7ERb7dEeBTbGF3mEz9%2Bgkn3oaW5VK3r8I9hXEgRPDoxB9VToS4yOXz81Cf1E8L1w6v%2Fi%2BxsRUDuL066LPSaKufwj7y3wjJ6W78Lj50N78E5y0LiBvI%3D&platform=android&ts=1517641252964&username=test&sign=ed0b636c8b14bf61678ebe91e01f0976 http/1.1
Content-Length: 0
Host: passport.bilibili.com
Connection: Keep-Alive
Accept-Encoding: gzip
User-Agent: okhttp/3.8.0
--> END POST (0-byte body)
<-- 200 OK https://passport.bilibili.com/api/oauth2/login?appkey=1d8b6e7d45233436&build=515000&mobi_app=android&password=a420%2Bl1tgoSbdLNA7GhrzXexWtp19LSf5ixbX8mmA7DIVr2Qg8bd8yEsO7ERb7dEeBTbGF3mEz9%2Bgkn3oaW5VK3r8I9hXEgRPDoxB9VToS4yOXz81Cf1E8L1w6v%2Fi%2BxsRUDuL066LPSaKufwj7y3wjJ6W78Lj50N78E5y0LiBvI%3D&platform=android&ts=1517641252964&username=test&sign=ed0b636c8b14bf61678ebe91e01f0976 (113ms)
Date: Sat, 03 Feb 2018 07:00:53 GMT
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Server: Apache-Coyote/1.1
Set-Cookie: sid=8th389hu; Domain=.bilibili.com; Expires=Sun, 03-Feb-2019 07:00:53 GMT; Path=/
X-Upstream: 172.18.17.72:80
X-TKID: 1517641253553338161090
Expires: Sat, 03 Feb 2018 07:00:52 GMT
Cache-Control: no-cache
X-Cache: BYPASS from cn-jlcc-cu-w-01.hdslb.com
Content-Encoding: gzip
<-- END HTTP (encoded body omitted)

loginResponseEntity就下面这些东西

login -400
data null
message 请求错误,请重试
ts 1517641084
czp3009 commented 6 years ago

好像确实没有什么区别 >.>

最近过年比较忙,等我空一些了我到 Android 上调试一下。

非常感谢提交 issue.

czp3009 commented 6 years ago

经过测试, 我发现在 Android 上进行 RSA 加密后, 密文会比在 OpenJDK 上多两位. 所以密文是不对的, 导致 B站 服务端无法解码, 从而返回 400.

在 StackOverflow 找到了类似问题, 需要将原来的

Cipher cipher = Cipher.getInstance("RSA");

改为

Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");

才能在 JRE 和 Android 都正常运行.

image

已经修正此问题, 在最新版本 0.0.7 中. 代码已经提交到 OSS, 过一会就同步了.

SakuraKoi commented 6 years ago

非常感谢!