文档转换成 pdf出现403http状态码 并且似乎没有出现error code

阅读次数 154

在调用文档转换成 pdf功能时response返回403并且postman中提示 { "successful": false,
"redirect": false
}

 OkHttpClient client = new OkHttpClient();
        MediaType mediaType = MediaType.parse("application/json");
        // 从 fileNameMap 中获取 fileid 对应的文档 URL
        String fileUrl = fileNameMap.get(fileid);
        // 提取文档 URL 中的文件名
        String fileName = fileUrl.substring(fileUrl.lastIndexOf("/") + 1);
        // 创建请求体 JSON
        String jsonBody = "{\"url\":\"" + fileUrl + "\",\"filename\":\"" + fileName + "\"}";
        RequestBody body = RequestBody.create(mediaType, jsonBody);
        Map paramMap = new HashMap();
        paramMap.put("_w_appid", ApplicationProperties.appid);
        paramMap.put("_w_fileid", fileid);
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        // 设置时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
        // 将当前时间转换为指定时区的格式化字符串
        String formattedTime = now.atZone(ZoneId.of("GMT")).format(formatter);
        String contentMd5 = Objects.requireNonNull(Signature.calculateContentMd5(jsonBody)).toLowerCase();
//        formattedTime = "Wed, 23 Jan 2013 06:43:08 GMT";
        // 构建请求对象
        Request request = new Request.Builder()
                .url("https://solution.wps.cn/api/developer/v1/office/convert/to/pdf")
                .post(body)
                .addHeader("Date", formattedTime)
                .addHeader("Content-Md5", contentMd5)
                .addHeader("Content-Type", "application/json")
                .addHeader("Authorization", "WPS-2:" + ApplicationProperties.appid + ":" + Signature.calculateSignature(ApplicationProperties.appSecret, contentMd5, "application/json", formattedTime));
1 Answers

您好,你可以先将开放平台的 response 中的 body 打印出来,通常 body 里会包含具体的错误信息。

看到了Response Body: {"code":30003,"message":"InvalidSignature","hint":"not a valid datetime or too skewed. Date: Thu, 29 Jun 2023 17:23:06 GMT","extra":""}这个错误为什么呢

签名生成的方法去生产案例的签名是相同的

public static String calculateSignature(String APPKEY, String Content_Md5, String Content_Type, String Date) { String input = APPKEY + Content_Md5 + Content_Type + Date; if (StringUtils.isEmpty(input)) {
return null;
} else {
return DigestUtils.sha1Hex(input);
}
}