- * 登录开放平台官网
- * 申请账号认证并接入
- * 创建应用
- * 获取Appid、ak、sk和Signkey
-
- this.uuid = UUID.randomUUID().toString().toUpperCase();
- String url2GetQr = "https://passport.baidu.com/v2/api/getqrcode?" + "lp=pc&qrloginfrom=pc&gid=" + this.uuid
- + "&apiver=v3";
-
- HttpGet get = new HttpGet(url2GetQr);
- HttpResponse res = client.execute(get);
- String content = EntityUtils.toString(res.getEntity(), "utf-8");
- JSONObject json = JSONObject.parseObject(content);
- this.imagUrl = json.getString("imgurl");
- this.sign = json.getString("sign");
-
- String url = "https://passport.baidu.com/channel/unicast?channel_id=" + this.sign + "&tpl=dev&gid=" + this.uuid
- + "&callback=&apiver=v3";
- HttpGet get = new HttpGet(url);
- HttpResponse res = client.execute(get);
- String content = EntityUtils.toString(res.getEntity(), "utf-8");
- JSONObject json = JSONObject.parseObject(content);
- int errno = json.getInteger("errno");
- if (errno != 0) {
- System.out.println("还未扫码");
- return false;
- }
- json = JSONObject.parseObject(json.getString("channel_v"));
- int status = json.getInteger("status");
- if (status != 0) {
- System.out.println("二维码已扫描,点击确认即可");
- return true;
- }
- this.bduss = json.getString("v");
-
- String url = "https://passport.baidu.com/v2/api/bdusslogin?tt=1620136625392" + "&bduss=" + bduss
- + "&u=https%253A%252F%252Fopenapi.baidu.com%252Foauth%252F2.0%252Fauthorize%253Fresponse_type%253Dcode%2526client_id%253Dil7RmGmgWExN8uKm95OkjGyevzUWFOVZ%2526redirect_uri%253Doob%2526scope%253Dbasic%252Cnetdisk%2526display%253Dtv%2526qrcode%253D1"
- + "&qrcode=1&tpl=dev&apiver=v3" + "&tt=1620136625392&traceid=&time=1620136625&alg=v3&"
- + "sig=d1kwUzNMVGc4aGRXekhWd21BYmh4bmlibnY5VTF2SU9nL2lDUHg2OWpOWEtEalB6WEtwZEgvV1ZmL2M0dUJDeA%3D%3D"
- + "&elapsed=34&shaOne=00a8413a841efcc76a027f832b86f0abf4b5f95a";
- HttpGet get = new HttpGet(url);
- HttpResponse res = client.execute(get);
- String content = EntityUtils.toString(res.getEntity(), "utf-8");
- Document html = Jsoup.parse(content);
- this.code = html.getElementById("Verifier").attr("value");
-
- String url = "https://openapi.baidu.com/oauth/2.0/token?grant_type=authorization_code"
- + "&code=" + this.code
- + "&client_id=" + this.appKey
- + "&client_secret=" + this.secretkey
- + "&redirect_uri=" + this.redirectUri;
- HttpGet get = new HttpGet(url);
- HttpResponse res = client.execute(get);
- String content = EntityUtils.toString(res.getEntity(), "utf-8");
- JSONObject json = JSONObject.parseObject(content);
- this.refreshToken = json.getString("refresh_token");
- this.accessToken = json.getString("access_token");
-
ps:爬虫获取到的页面,可能和浏览器直接访问的页面不一样,因为页面获取可能为异步加载或者异步请求,所以爬虫直接访问的url可能不是真实的url。
解决: