Baidu云盘【文件API】接口学习 『一』
这几天很有兴致的学习了百度云盘文件API接口的使用;初步是想做一个在线android应用,应用中的文档是存放在百度云盘的。
主要是分一下几个步骤:
1.注册百度账号
2.登录百度开发者中心
3.创建移动应用,获取对应的(API Key Secret Key)
4.开通pcs API权限
5.获取ACCESS_token(认证编码)
6.开发应用
注意:
开通移动应用,获取key
获取token的时候我使用的安卓获取的方式
通过我写对应api的例子我发现,其实就两种情况:一种是get方式提交数据,另外一种是post方式提交数据
1.get方式提交数据,我们用获取云盘的信息为例:
获取云盘信息前我们要知道,我们要准备好什么数据:
请求参数:
url: 标明我们要访问的网址路径 值固定问“https://pcs.baidu.com/rest/2.0/pcs/quota”
method:标明我们是请求云盘信息 值固定为“info”
acceess_token:准入标识 值是我们自己申请的
接收返回参数:
quota:云盘总容量
used:云盘使用容量
request_id:该请求的表示,没啥用
返回的一个json串如下格式:{"quota":123794882560, "used":83573494688,"request_id":2853739529}
我在做的时候你使用Gson工具将json串转换到对应的entity类中了 代码如下:
[html] /**
* @param URLConnection conn通过get方式获取StringBuffer
* @return
*/
private StringBuffer getJsonString(URLConnection conn) {
InputStreamReader isr = null;
BufferedReader br = null;
StringBuffer sb = null;
try {
isr = new InputStreamReader(conn.getInputStream(),"gb2312");
br = new BufferedReader(isr);
String line = null;
sb = new StringBuffer();
while ((line = br.readLine()) != null) {
sb.append(line);
sb.append("\r\n");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(isr!=null)
isr.close();
} catch (IOException e) {
System.out.println("流关闭是异常");
e.printStackTrace();
}
}
return sb;
}
/**
* @return
* @throws Exception
* 获取云空间的信息
*/
public CloudInfo getCloudInfo() throws Exception {
URL u = new URL(https://pcs.baidu.com/rest/2.0/pcs/quota"?method=info&access_token=你申请的token的值";
URLConnection conn = u.openConnection();// 打开网页链接
// 获取用户云盘信息
String cloudJson = this.getJsonString(conn).toString();
// 解析成对象 下面有这个实体对象的类
Gson gson = new Gson();
CloudInfo cloudInfo = gson.fromJson(cloudJson, CloudInfo.class);
System.out.println("云盘信息:"+cloudInfo);
return cloudInfo;
}
/**
* @param URLConnection conn通过get方式获取StringBuffer
* @return
*/
private StringBuffer getJsonString(URLConnection conn) {
InputStreamReader isr = null;
BufferedReader br = null;
StringBuffer sb = null;
try {
isr = new InputStreamReader(conn.getInputStream(),"gb2312");
br = new BufferedReader(isr);
String line = null;
sb = new StringBuffer();
while ((line = br.readLine()) != null) {
sb.append(line);
sb.append("\r\n");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(isr!=null)
isr.close();
} catch (IOException e) {
System.out.println("流关闭是异常");
e.printStackTrace();
}
}
return sb;
}
/**
* @return
* @throws Exception
* 获取云空间的信息
*/
public CloudInfo getCloudInfo() throws Exception {
URL u = new URL(https://pcs.baidu.com/rest/2.0/pcs/quota"?method=info&access_token=你申请的token的值";
URLConnection conn = u.openConnection();// 打开网页链接
// 获取用户云盘信息
String cloudJson = this.getJsonString(conn).toString();
// 解析成对象 下面有这个实体对象的类
Gson gson = new Gson();
CloudInfo cloudInfo = gson.fromJson(cloudJson, CloudInfo.class);
System.out.println("云盘信息:"+cloudInfo);
return cloudInfo;
}
[html] package com.entity;
import java.lang.reflect.Type;
/**
* @author ydcun 获取云空间的信息 例如:
* {"quota":123794882560, 空间配额,单位为字节
* "used":83573494688, 已使用空间大小 单位为字节。
* "request_id":2853739529}
*/
public class CloudInfo{
private Double quota;
private Double used;
private Double request_id;
/**
* @return the quota 空间配额,单位为字节
&
补充:软件开发 , Java ,