How to post a file in contact's chat using java?

Andrea
Mon, 20 Feb 2023 17:03:44 +0000
Chat

I’m trying to send a file in a contact’s chat using oauth server-to-server token (bearer).
I’ve tried using simple java classes but I always receive 405 Method Not Allowed error.
Here’s my code that uses apache http client.

import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class ZoomNewMultipartFileSend {
	private static final String API_ENDPOINT = "https://api.zoom.us/v2/chat/users/";
	public static void main(String[] args) {
		String token = ZoomApi.getToken();//get a token using oauth server-to-server
		String userId = "my userid";
		String toContact = "rec.contact userid";
		String filePath = "path to my file with extension";
		try {
			CloseableHttpClient httpClient = HttpClients.createDefault();
			HttpPost uploadFile = new HttpPost(API_ENDPOINT+userId+"/messages/files");
			uploadFile.setHeader(HttpHeaders.AUTHORIZATION,"Bearer "+token);
			MultipartEntityBuilder builder = MultipartEntityBuilder.create();
			builder.addTextBody("to_contact",toContact);
			File f=new File(filePath);
			builder.addBinaryBody("files",f,ContentType.APPLICATION_OCTET_STREAM, filePath);
			HttpEntity multipart = builder.build();
			uploadFile.setEntity(multipart);
			CloseableHttpResponse response = httpClient.execute(uploadFile);
			HttpEntity responseEntity = response.getEntity();
			<read response>
		}catch(Exception e) {e.printStackTrace();}
	}
}

9 posts - 2 participants

Read full topic