Java调用Zoom API发送文件遇405错误求助
问题描述
已完成Zoom OAuth服务端到服务端认证,应用拥有/chat_message:write:admin权限,且能正常发送普通聊天消息。持有有效token、自身用户ID和接收方ID,但用Java程序发送label.pdf文件时,返回405 Method Not Allowed错误。
相关代码
private static final String API_ENDPOINT = "https://api.zoom.us/v2/chat/users/"; // Create API URL URL url = new URL(API_ENDPOINT + userId + "/messages/files"); // Open connection and set request properties HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Authorization", "Bearer " + token); conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=boundary"); byte[] pdfData = Files.readAllBytes(new File(filePath).toPath()); String boundary = "boundary"; String delimiter = "--" + boundary + "\n"; StringBuilder builder = new StringBuilder(); builder.append(delimiter); builder.append("Content-Disposition: form-data; name=\"to_contact\"\n\n"); builder.append(toContact+"\n"); builder.append(delimiter); builder.append("Content-Disposition: form-data; name=\"files\"; filename=\"label\"\n"); builder.append("Content-Type: application/pdf\n"); builder.append("\n"); builder.append(new String(pdfData, "ISO-8859-1")); builder.append("\n"); builder.append("--" + boundary + "--"); conn.setDoOutput(true); String multipartData = builder.toString(); System.out.println(multipartData); conn.getOutputStream().write(multipartData.getBytes("UTF-8")); // Send request and get response code and message conn.connect(); int responseCode = conn.getResponseCode(); String responseMessage = conn.getResponseMessage(); // Check if response is successful if (Arrays.asList(RESPONSE_CODE).contains(Integer.toString(responseCode))) { System.out.println("File sent successfully!"); System.out.println("Response code: " + responseCode); System.out.println("Response message: " + responseMessage); } else { System.out.println("Error sending file:"); System.out.println("Response code: " + responseCode); System.out.println("Response message: " + responseMessage); } // Close connection conn.disconnect();
问题排查与解决
API端点错误:发送聊天文件的正确Zoom API端点是
https://api.zoom.us/v2/chat/users/{userId}/messages,你代码中使用的/messages/files是无效路径,服务器无法识别该路径的POST请求,因此返回405错误。二进制数据处理问题:将PDF二进制数据转成
ISO-8859-1字符串再转成UTF-8字节的操作会损坏二进制文件,正确做法是直接写入原始二进制数据,避免字符串中转。边界一致性优化:虽然不是405错误的直接原因,但要确保
Content-Type头中指定的boundary值与实际构建表单时使用的分隔符完全一致,避免后续表单解析失败。
修正后的关键代码示例
修正端点
URL url = new URL(API_ENDPOINT + userId + "/messages");
正确处理二进制数据写入
OutputStream os = conn.getOutputStream(); // 写入收件人参数 os.write(delimiter.getBytes("UTF-8")); os.write("Content-Disposition: form-data; name=\"to_contact\"\n\n".getBytes("UTF-8")); os.write((toContact + "\n").getBytes("UTF-8")); // 写入文件部分 os.write(delimiter.getBytes("UTF-8")); os.write("Content-Disposition: form-data; name=\"files\"; filename=\"label.pdf\"\n".getBytes("UTF-8")); os.write("Content-Type: application/pdf\n\n".getBytes("UTF-8")); // 直接写入PDF二进制数据,不经过字符串转换 os.write(pdfData); // 写入结束边界 os.write(("\n--" + boundary + "--").getBytes("UTF-8")); os.flush();
内容的提问来源于stack exchange,提问作者AndreaTaroni86
相关产品推荐
相关产品推荐

