百度云 api java_如何使用百度云API接口

展开全部http://developer.baidu.com/wiki/index.php?title=%E5%B8%AE%E5%8A%A9%E6%96%87%E6%A1%A3%E9%A6%96%E9%A1%B5/%E8%B5%84%E6%BA%90%E4%B8%8B%E8%BD%BD学习了百度云盘文件API接口的使用;初步是想做一个在线android应用323131333532363134313...
摘要由CSDN通过智能技术生成

展开全部

http://developer.baidu.com/wiki/index.php?title=%E5%B8%AE%E5%8A%A9%E6%96%87%E6%A1%A3%E9%A6%96%E9%A1%B5/%E8%B5%84%E6%BA%90%E4%B8%8B%E8%BD%BD

学习了百度云盘文件API接口的使用;初步是想做一个在线android应用32313133353236313431303231363533e78988e69d8331333361326238,应用中的文档是存放在百度云盘的。

主要是分一下几个步骤:

1.注册百度账号

2.登录百度开发者中心

3.创建移动应用,获取对应的(API Key Secret Key)

4.开通pcs API权限

5.获取ACCESS_token(认证编码)

6.开发应用

注意:

开通移动应用,获取key

获取token的时候我使用的安卓获取的方式

通过我写对应api的例子我发现,其实就两种情况:一种是get方式提交数据,另外一种是post方式提交数据

1.get方式提交数据,我们用获取云盘的信息为例:

获取云盘信息前我们要知道,我们要准备好什么数据:

请求参数:

url: 标明我们要访问的网址路径 值固定问""

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("?method=info&access_token=你申请的token的值";

URLConne

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很高兴扮演ChitGPT,接下来我将为您提供有关如何使用Java调用百度云API查询域名到期时间的详细说明。 首先,您需要申请一个百度云账号并在控制台中创建一个新的应用程序,然后在应用程序中启用“域名API”服务。接着,您需要获取您的Access Key和Secret Key,这将用于与百度云API进行身份验证和授权。 在获得这些凭证之后,您可以使用Java中的HttpClient类来向百度API发送GET请求并获取JSON响应。以下为示例代码: ```java import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class DomainExpirationQuery { private static final String API_URL = "https://domainapi.baidu.com/api/v3/domain/expiredate"; public static void main(String[] args) { String domainName = "example.com"; String accessKeyId = "yourAccessKeyId"; String secretKey = "yourSecretKey"; try { URL url = new URL(API_URL + "?apikey=" + accessKeyId + "&domain=" + domainName); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setRequestProperty("Content-Type", "application/json"); conn.setRequestProperty("User-Agent", "Mozilla/5.0"); conn.setRequestProperty("X-bce-date", "20220101T010101Z"); String auth = AuthUtils.generateAuthorization(accessKeyId, secretKey, "GET", url.toString(), null, conn.getRequestProperty("X-bce-date")); conn.setRequestProperty("Authorization", auth); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); System.out.println(response.toString()); } catch (Exception e) { System.out.println("Error: " + e.getMessage()); } } } ``` 在上面的示例代码中,您需要将“example.com”替换为您要查询到期时间的域名,将“yourAccessKeyId”和“yourSecretKey”替换为您的百度云凭证。 请注意,您还需要在AuthUtils类中实现generateAuthorization方法以生成用于身份验证的Authorization头。这需要使用HMAC-SHA256算法和UTF-8字符集生成签名,并对其进行Base64编码。以下为示例代码: ```java import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; import java.util.SimpleTimeZone; public class AuthUtils { private static final String BCE_AUTH_VERSION = "bce-auth-v1"; private static final String BCE_DATE_FORMAT = "yyyyMMdd'T'HHmmss'Z'"; private static final String BCE_TIME_ZONE = "UTC"; public static String generateAuthorization(String accessKeyId, String secretAccessKey, String httpMethod, String uri, String params, String timestamp) { try { String authString = BCE_AUTH_VERSION + "/" + accessKeyId + "/" + getFormattedTimestamp(timestamp) + "/1800"; String httpHeaders = ""; String httpQuery = (params != null && !params.isEmpty()) ? "?" + params : ""; String signingKey = generateSigningKey(secretAccessKey, getFormattedTimestamp(timestamp), "bce/auth/v1"); String auth = generateAuthString(authString, httpMethod.toUpperCase(), uri, httpQuery, httpHeaders, timestamp, signingKey); return auth; } catch (Exception e) { throw new RuntimeException("Unable to generate authorization signature", e); } } private static String generateAuthString(String authString, String httpMethod, String uri, String httpQuery, String httpHeaders, String timestamp, String signingKey) throws NoSuchAlgorithmException { String canonicalRequest = httpMethod + "\n" + uri + httpQuery + "\n" + httpQuery + "\n" + httpHeaders + "\n\n" + "content-md5;content-type\n" + timestamp + "\n" + "x-bce-date:" + timestamp + "\n\n" + "content-md5;content-type;x-bce-date\n" + "UNSIGNED-PAYLOAD"; String signing = hmacSha256(signingKey, canonicalRequest); String auth = authString + "/" + "host" + "/" + signing; return auth; } private static String hmacSha256(String secret, String message) { try { byte[] key = secret.getBytes(StandardCharsets.UTF_8); SecretKeySpec secretKeySpec = new SecretKeySpec(key, "HmacSHA256"); Mac mac = Mac.getInstance("HmacSHA256"); mac.init(secretKeySpec); byte[] messageBytes = message.getBytes("UTF-8"); byte[] result = mac.doFinal(messageBytes); return Base64.encodeBase64String(result); } catch (Exception e) { throw new RuntimeException("Unable to calculate a request signature: " + e.getMessage(), e); } } private static String generateSigningKey(String secretAccessKey, String dateStamp, String regionName) throws Exception { byte[] kSecret = ("Baidu" + secretAccessKey).getBytes(StandardCharsets.UTF_8); byte[] kDate = hmacSha256("Baidu" + dateStamp, kSecret).getBytes(StandardCharsets.UTF_8); byte[] kRegion = hmacSha256(regionName, kDate).getBytes(StandardCharsets.UTF_8); byte[] kService = hmacSha256("bce/auth/v1", kRegion).getBytes(StandardCharsets.UTF_8); byte[] kSigning = hmacSha256("bce/auth/v1", kService).getBytes(StandardCharsets.UTF_8); return Base64.encodeBase64String(kSigning); } private static String getFormattedTimestamp(String timestamp) { SimpleDateFormat sdf = new SimpleDateFormat(BCE_DATE_FORMAT, Locale.US); sdf.setTimeZone(new SimpleTimeZone(0, BCE_TIME_ZONE)); if (timestamp == null) { return sdf.format(new Date()); } Calendar cd = Calendar.getInstance(); try { Date date = sdf.parse(timestamp); cd.setTime(date); } catch (Throwable ignored) { } return sdf.format(cd.getTime()); } } ``` 最后,代码将输出响应的JSON字符串,其中包含查询到域名到期日期的信息。 希望这个例子能帮助您理解如何使用Java调用百度云API来查询域名到期时间!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值