java 调用百度网盘开放api接口:通过httpclient获取access_token


1、百度网盘开放平台注册
* 登录开放平台官网
* 申请账号认证并接入
* 创建应用
* 获取Appid、ak、sk和Signkey
2、获取授权
  • 官网描述

  • 获取到登录二维码的一些信息,并将这个uuid保存记录。后面需要通过uuid和sign检查二维码是否扫描。

    this.uuid = UUID.randomUUID().toString().toUpperCase();
    String url2GetQr = "https://passport.baidu.com/v2/api/getqrcode?" + "lp=pc&qrloginfrom=pc&gid=" + this.uuid
    				+ "&apiver=v3";
    
  • 使用httpclient访问上一步的url,在得到的json数据中,得到真正的二维码地址,可以下载到本地或者其他使用方式。还需要将json数据中的sign的值取出,判断二维码是否扫描需要用到。

    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");
    
  • 检查二维码是否扫描,error不为0就是还没有扫描的,status不为0就是扫了二维码还没点击。同时记录返回数据中的bduss值,后面获取code需要用到。

    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");
    
  • 通过上面获取到的bduss值,获取code值,code的值10分钟内有效。同时将获取到的code值记录下来,后面用他获取tocken。

    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");
    
  • 利用自己在开放平台申请到的appid,secrekey等信息,和上一步获取到的code值获取tocken值

    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");
    
  • 获取到tocken的值后,记录存储下来,就可以进行百度网盘开放的api操作,详细的一些操作,官网都有介绍。 入口

ps:爬虫获取到的页面,可能和浏览器直接访问的页面不一样,因为页面获取可能为异步加载或者异步请求,所以爬虫直接访问的url可能不是真实的url。

解决:

在这里插入图片描述

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
这个错误通常意味着你的Access Token无效或过期了。你需要获取一个有效的Access Token调用百度OCR接口。 以下是一种获取Access Token的方法: ```java import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.utils.URLEncodedUtils; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; public class BaiduOCR { public static void main(String[] args) throws IOException { // 设置API Key和Secret Key String apiKey = "your_api_key"; String secretKey = "your_secret_key"; // 获取访问令牌 String accessToken = getAccessToken(apiKey, secretKey); // 调用百度OCR接口 String result = callBaiduOCR(accessToken); System.out.println(result); } // 获取访问令牌 private static String getAccessToken(String apiKey, String secretKey) throws IOException { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost("https://aip.baidubce.com/oauth/2.0/token"); List<NameValuePair> params = new ArrayList<>(); params.add(new BasicNameValuePair("grant_type", "client_credentials")); params.add(new BasicNameValuePair("client_id", apiKey)); params.add(new BasicNameValuePair("client_secret", secretKey)); httpPost.setEntity(new UrlEncodedFormEntity(params)); HttpResponse response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); String result = EntityUtils.toString(entity, StandardCharsets.UTF_8); httpClient.close(); return result.split("\"")[3]; } // 调用百度OCR接口 private static String callBaiduOCR(String accessToken) throws IOException { // 调用百度OCR接口的代码 return ""; } } ``` 在这个示例代码中,你需要将`your_api_key`和`your_secret_key`替换为你自己的百度API Key和Secret Key。在`getAccessToken`方法中,我们使用了HTTP POST请求来获取访问令牌,并从响应中提取出Access Token。 在调用百度OCR接口的方法中,你需要将获取到的Access Token传递给接口调用,以确保你使用了有效的访问令牌。 请确保你已经按照这个方法获取了有效的Access Token并传递给了百度OCR接口调用。 希望这能解决你的问题!如果还有其他疑问,请随时提问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值