我正在尝试使用slack sdk for java和file.upload method将文件上传到slack
我尝试过使用.file()和.fileData(),但在控制台中没有收到任何错误,事实上,在调用这两个方法中的任何一个之后,似乎都没有输出任何内容。使用content("something")方法时,我只得到"All ok“和松弛的消息。
下面是我的代码:
File report = new File(System.getProperty("user.dir") + "\\report.zip");
if(!report.exists()) {
System.out.println("Report Zip Doesnt Exist");
return false;
}
FilesUploadRequest request = FilesUploadRequest.builder()
.channels(channels)
.initialComment(comment)
.filename(report.getName())
//.file(report) doesnt work
//.fileData(FileUtils.readFileToByteArray(report)) doesnt work
.content("something") // works
.title("Report")
.filetype("zip")
.build();
try {
FilesUploadResponse response = methods.filesUpload(request);
if (response.isOk()) {
System.out.println("All ok");
return true;
} else {
System.out.println(response.getError());
return false;
}
} catch (SlackApiException requestFailure) {
System.out.println("Request failed");
return false;
} catch (IOException connectivityIssue) {
System.out.println("Connectivity issue");
return false;
}发布于 2021-02-17 01:16:06
有一种方法
在slack ->中创建您的应用程序转到添加特性和功能->转到权限->转到范围-> Bot令牌作用域->使用生成的files.write ->令牌添加Oauth作用域
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.setBearerAuth(token); // pass generated token here
MultiValueMap<String, Object> bodyMap = new LinkedMultiValueMap<>();
bodyMap.add("file", bos.toByteArray(),fileName )); // convert file to ByteArrayOutputStream and pass with toByteArray() or pass with new File()
bodyMap.add("initial_comment", message); // pass comments with file
bodyMap.add("channels", "XXXXXX"); // pass channel codeID
HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(bodyMap, headers);
ResponseEntity<Object> responseEntity = restTemplate.postForEntity("https://slack.com/api/files.upload", entity,
Object.class);
System.out.println("sendEmailWithAttachment() response status: "+ responseEntity.getStatusCode().toString());https://stackoverflow.com/questions/63126117
复制相似问题