5

百度登陆方法及网盘 API 基本操作

 3 years ago
source link: https://blog.lilydjwg.me/2014/3/26/log-into-baidu-with-curl-and-pan-api-demo.44021.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

百度登陆方法及网盘 API 基本操作

本文来自依云's Blog,转载请注明。

本文只介绍流程,因此是以最方便试错的 shell 脚本为示例的。也就是一系列简单的 HTTP 请求,用什么语言都一样。

要实践本文中的例子,首先要确保系统上已经安装了如下软件:

  • curl: 命令行 HTTP 调试首选工具
  • jshon: 命令行 JSON 解析器。使用简单的栈式语法
  • json_pp: 这个命令行工具是 perl 自带的,把 JSON 数据格式化显示用的

首先把用户信息存到变量里去:

$ BDUSER=你的百度登陆名
$  PASS=你的百度登陆密码

访问一次百度,取得一个名为BAIDUID的 cookie。我们在此,以及以下所有 curl 命令中,都会使用-b-c选项告诉 curl 从当前目录下的「cookies」文件读取 cookie 数据,把接收到的 cookie 写到同一个文件里去。

$ curl -b cookies -c cookies http://www.baidu.com/ -sS -o /dev/null

获取 token:

$ TOKEN=$(curl -b cookies -c cookies -sS "https://passport.baidu.com/v2/api/?getapi&tpl=mn&apiver=v3&class=login&tt=$(date +%s860)&logintype=dialogLogin" | tr "'" '"' | jshon -e data -e token -u)
$ curl -b cookies -c cookies "https://passport.baidu.com/v2/api/?logincheck&token=$TOKEN&tpl=mn&apiver=v3&tt=$(date +%s)&username=$BDUSER&isphone=false"
{"errInfo":{ "no": "0" }, "data": { "codeString" : "", "vcodetype" : "" }}

使用用户信息登陆:

$ curl -b cookies -c cookies --compressed -sS 'https://passport.baidu.com/v2/api/?login' -H 'Content-Type: application/x-www-form-urlencoded' --data "staticpage=http%3A%2F%2Fpan.baidu.com%2Fres%2Fstatic%2Fthirdparty%2Fpass_v3_jump.html&charset=utf-8&token=$TOKEN&tpl=mn&apiver=v3&tt=$(date +%s083)&codestring=&safeflg=0&u=http%3A%2F%2Fpan.baidu.com%2F&isPhone=false&quick_user=0&logintype=basicLogin&username=$BDUSER&password=$PASS&verifycode=&mem_pass=on&ppui_logintime=57495&callback=parent.bd__pcbs__ax1ysj" | grep -F 'err_no=400032' > /dev/null

如果这条命令返回(即$?的值)为 0 则成功,否则失败。这是因为百度的登陆之后的页面会进行跳转,如果登陆成功那么跳转 URL 里包含err_no=400032,否则err_no是别的什么值。这个判断条件可能会变化,比如去年年底是err_no=40032时表示成功。

登陆成功之后就可以调用网盘 API 了。这部分比登陆的请求要好看许多。

先来获取一下网盘容量,也好确认我们确实登陆成功了:

$ curl -b cookies -c cookies 'http://pan.baidu.com/api/quota'
{"errno":0,"total":510341939200,"used":0}

这些请求直接在网页版里开着 Firebug 看就可以了。比如:

列根目录下的文件信息:

建立目录:

$ curl -b cookies -c cookies 'http://pan.baidu.com/api/create' -F path=/测试curl -F isdir=1 -F size= -F block_list='[]' -F method=post

发送到 Kindle


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK