当前位置 : 主页 > 编程语言 > python >

全网开发都可搭建技术数据库篇2_如何调用没有文档说明的 Web API

来源:互联网 收集:自由互联 发布时间:2022-06-18
大家好!几天前我写了​​全网开发BC都可搭建技术数据库篇2_Linux系统MySQL配置主从分离​​的文章,里面提到了调用搭建菠菜文档说明的“秘密” API 很有意思,你需要从你的浏览器中

大家好!几天前我写了​​全网开发BC都可搭建技术数据库篇2_Linux系统MySQL配置主从分离​​的文章,里面提到了调用搭建菠菜文档说明的“秘密” API 很有意思,你需要从你的浏览器中把 cookies 复制出来才能访问。

有些读者问如何实现,因此我打算详细描述下,其实过程很简单。我们还会谈谈在调用没有文档说明的 API 时,可能会遇到的错误和道德问题。

我们用谷歌 Hangouts 举例。我之所以选择它,并不是因为这个例子最有用(我认为官方的 API 更实用),而是因为在这个场景中更有用的网站很多是小网站,而小网站的 API 一旦被滥用,受到的伤害会更大。因此我们使用谷歌 Hangouts,因为我 100% 肯定谷歌论坛可以抵御这种试探行为。

我们现在开始!

第一步:打开开发者工具,找一个 JSON 响应

我浏览了 ​​https://hangouts.google.com​​,在 Firefox 的开发者工具中打开“网络Network”标签,找到一个 JSON 响应。你也可以使用 Chrome 的开发者工具。

打开之后界面如下图:

全网开发都可搭建技术数据库篇2_如何调用没有文档说明的 Web API_python

找到其中一条 “类型Type” 列显示为 ​​json​​ 的请求。

为了找一条感兴趣的请求,我找了好一会儿,突然我找到一条 “people” 的端点,看起来是返回我们的联系人信息。听起来很有意思,我们来看一下。

第二步:复制为 cURL

下一步,我在感兴趣的请求上右键,点击 “复制Copy” -> “复制为 cURLCopy as cURL”。

然后我把 ​​curl​​ 命令粘贴到终端并运行。下面是运行结果:

$ curl 'https://people-pa.clients6.google.com/v2/people/?key=REDACTED' -X POST ........ (省略了大量请求标头)

Warning: Binary output can mess up your terminal. Use "--output -" to tell

Warning: curl to output it to your terminal anyway, or consider "--output

Warning: <FILE>" to save to a file.

你可能会想 —— 很奇怪,“二进制的输出在你的终端上无法正常显示” 是什么错误?原因是,浏览器默认情况下发给服务器的请求头中有 ​​Accept-Encoding: gzip, deflate​​ 参数,会把输出结果进行压缩。

我们可以通过管道把输出传递给 ​​gunzip​​ 来解压,但是我们发现不带这个参数进行请求会更简单。因此我们去掉一些不相关的请求头。

第三步:去掉不相关的请求头

下面是我从浏览器获得的完整 ​​curl​​ 命令。有很多行!我用反斜杠(​​\​​)把请求分开,这样每个请求头占一行,看起来更清晰:

curl 'https://people-pa.clients6.google.com/v2/people/?key=REDACTED' \

-X POST \

-H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:96.0) Gecko/20100101 Firefox/96.0' \

-H 'Accept: */*' \

-H 'Accept-Language: en' \

-H 'Accept-Encoding: gzip, deflate' \

-H 'X-HTTP-Method-Override: GET' \

-H 'Authorization: SAPISIDHASH REDACTED' \

-H 'Cookie: REDACTED'

-H 'Content-Type: application/x-www-form-urlencoded' \

-H 'X-Goog-AuthUser: 0' \

-H 'Origin: https://hangouts.google.com' \

-H 'Connection: keep-alive' \

-H 'Referer: https://hangouts.google.com/' \

-H 'Sec-Fetch-Dest: empty' \

-H 'Sec-Fetch-Mode: cors' \

-H 'Sec-Fetch-Site: same-site' \

-H 'Sec-GPC: 1' \

-H 'DNT: 1' \

-H 'Pragma: no-cache' \

-H 'Cache-Control: no-cache' \

-H 'TE: trailers' \

--data-raw 'personId=101777723309&personId=1175339043204&personId=1115266537043&personId=116731406166&extensionSet.extensionNames=HANGOUTS_ADDITIONAL_DATA&extensionSet.extensionNames=HANGOUTS_OFF_NETWORK_GAIA_GET&extensionSet.extensionNames=HANGOUTS_PHONE_DATA&includedProfileStates=ADMIN_BLOCKED&includedProfileStates=DELETED&includedProfileStates=PRIVATE_PROFILE&mergedPersonSourceOptions.includeAffinity=CHAT_AUTOCOMPLETE&coreIdParams.useRealtimeNotificationExpandedAcls=true&requestMask.includeField.paths=person.email&requestMask.includeField.paths=person.gender&requestMask.includeField.paths=person.in_app_reachability&requestMask.includeField.paths=person.metadata&requestMask.includeField.paths=person.name&requestMask.includeField.paths=person.phone&requestMask.includeField.paths=person.photo&requestMask.includeField.paths=person.read_only_profile_info&requestMask.includeField.paths=person.organization&requestMask.includeField.paths=person.location&requestMask.includeField.paths=person.cover_photo&requestMask.includeContainer=PROFILE&requestMask.includeContainer=DOMAIN_PROFILE&requestMask.includeContainer=CONTACT&key=REDACTED'

第一眼看起来内容有很多,但是现在你不需要考虑每一行是什么意思。你只需要把不相关的行删掉就可以了。

我通常通过删掉某行查看是否有错误来验证该行是不是可以删除 —— 只要请求没有错误就一直删请求头。通常情况下,你可以删掉 ​​Accept*​​、​​Referer​​、​​Sec-*​​、​​DNT​​、​​User-Agent​​ 和缓存相关的头。

在这个例子中,我把请求删成下面的样子:

curl 'https://people-pa.clients6.google.com/v2/people/?key=REDACTED' \

-X POST \

-H 'Authorization: SAPISIDHASH REDACTED' \

-H 'Content-Type: application/x-www-form-urlencoded' \

-H 'Origin: https://hangouts.google.com' \

-H 'Cookie: REDACTED'\

--data-raw 'personId=101777723309&personId=1175339043204&personId=1115266537043&personId=116731406166&extensionSet.extensionNames=HANGOUTS_ADDITIONAL_DATA&extensionSet.extensionNames=HANGOUTS_OFF_NETWORK_GAIA_GET&extensionSet.extensionNames=HANGOUTS_PHONE_DATA&includedProfileStates=ADMIN_BLOCKED&includedProfileStates=DELETED&includedProfileStates=PRIVATE_PROFILE&mergedPersonSourceOptions.includeAffinity=CHAT_AUTOCOMPLETE&coreIdParams.useRealtimeNotificationExpandedAcls=true&requestMask.includeField.paths=person.email&requestMask.includeField.paths=person.gender&requestMask.includeField.paths=person.in_app_reachability&requestMask.includeField.paths=person.metadata&requestMask.includeField.paths=person.name&requestMask.includeField.paths=person.phone&requestMask.includeField.paths=person.photo&requestMask.includeField.paths=person.read_only_profile_info&requestMask.includeField.paths=person.organization&requestMask.includeField.paths=person.location&requestMask.includeField.paths=person.cover_photo&requestMask.includeContainer=PROFILE&requestMask.includeContainer=DOMAIN_PROFILE&requestMask.includeContainer=CONTACT&key=REDACTED'

这样我只需要 4 个请求头:​​Authorization​​、​​Content-Type​​、​​Origin​​ 和 ​​Cookie​​。这样容易管理得多。

网友评论