I'm trying to send a file using zoom api, and I have a OAuth server-to-server authentication with /chat_message:write:admin in my app scope and I have tested simple message without problems. I already have a token, my ID and my recipient's ID. My program runs and returns error 405 method not allowed. I choose to send label.pdf file. Zoom documentation page is: link to zoom api send document
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 + "\r\n";
StringBuilder builder = new StringBuilder();
builder.append(delimiter);
builder.append("Content-Disposition: form-data; name=\"to_contact\"\r\n\r\n");
builder.append(toContact+"\r\n");
builder.append(delimiter);
builder.append("Content-Disposition: form-data; name=\"files\"; filename=\"label\"\r\n");
builder.append("Content-Type: application/pdf\r\n");
builder.append("\r\n");
builder.append(new String(pdfData, "ISO-8859-1"));
builder.append("\r\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();
Can anyone suggest me what's wrong?
After few tries, I found a solution with OkHttpClient. I post it here if anyone needs it, hope it will help someone else struggling with this stuff. let's take for good you have already this part:
String accessToken = "TOKEN HERE";//your token
String recipientId = "v4iyWT1LTfy8QvPG4GTvdg"; // Recipient id here
File file = new File("C:\\MyPDF.pdf"); // path to your file
String myId = "xxxx_T1LTfy8QvPG4Gxxxx"; // my ID
Your url then will be:
String myURL = "https://file.zoom.us/v2/chat/users/"+myId+"/messages/files";
Now we can do the trick:
OkHttpClient client = new OkHttpClient.Builder()
.followRedirects(true)
.build();
// Creates Multipart request body
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("to_contact", recipientId)
RequestBody.create(file, MediaType.parse("application/pdf")))
.addPart(MultipartBody.Part.createFormData("files",file.getName(),RequestBody.create(file, MediaType.parse("application/pdf"))))
.build();
Request request = new Request.Builder()
.url(myURL)
.addHeader("Authorization", "Bearer " + accessToken)
.post(requestBody)
.build();
// Sends request and gets response
Response response = client.newCall(request).execute();
System.out.println("Response code: " + response.code());
response.close();
client.dispatcher().executorService().shutdown();
And... That's all, work like a charm :)