我尝试使用Box API将文件上传到Box。
但无论我尝试什么,我总是收到400 Bad Request而没有任何其他信息。
关于这个问题的任何想法?
API中的示例是此卷曲请求:
卷曲https://upload.box.com/api/2.0/files/content \
-H"授权:持票人ACCESS_TOKEN" -X POST \
-F attributes =' {" name":" tigers.jpeg"," parent":{" id":&# 34; 11446498"}}' \
-F file=@myfile.jpg
我的代码如下:
String URL = "https://upload.box.com/api/2.0/files/content/";
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(URL);
postMethod.setRequestHeader("Authorization", "Bearer "+ this.token);
try {
List<Part> parts = new ArrayList<Part>();
JSONObject parent = new JSONObject();
parent.put("id", this.parentId);
JSONObject attributes = new JSONObject();
attributes.put("parent", parent);
attributes.put("name", file.getName());
StringPart strPart = new StringPart("attributes", attributes.toString());
strPart.setContentType("application/json");
parts.add(strPart);
ByteArrayPartSource source = new ByteArrayPartSource(file.getName(),
IOUtils.toByteArray(this.file);
parts.add(new FilePart("file", source));
postMethod.setRequestEntity(new MultipartRequestEntity(parts.toArray(new Part[0]), postMethod.getParams()));
httpClient.executeMethod(postMethod);
int status = postMethod.getStatusCode();
if (status == HttpURLConnection.HTTP_OK || status == HttpURLConnection.HTTP_ACCEPTED) {
String jsonText = postMethod.getResponseBodyAsString();
JSONObject json = new JSONObject(jsonText);
System.out.println(jsonText);
} else {
throw new MyException(postMethod.getResponseBodyAsString());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
postMethod.releaseConnection();
}
答案 0 :(得分:1)
我发现了解决方案,不同部分不正确。 我必须创建3个部分:
此代码有效:
String URL = "https://upload.box.com/api/2.0/files/content";
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(URL);
postMethod.setRequestHeader("Authorization", "Bearer "+ this.token);
try {
List<Part> parts = new ArrayList<Part>();
parts.add(new StringPart("parent_id", parentId));
JSONObject parent = new JSONObject();
parent.put("id", this.parentId);
JSONObject attributes = new JSONObject();
attributes.put("parent", parent);
attributes.put("name", file.getName());
StringPart strPart = new StringPart("metadata", attributes.toString());
strPart.setContentType("text/plain");
parts.add(strPart);
ByteArrayPartSource source = new ByteArrayPartSource(file.getName(),
IOUtils.toByteArray(this.file));
parts.add(new FilePart("file", source));
postMethod.setRequestEntity(new MultipartRequestEntity(parts.toArray(new Part[0]), postMethod.getParams()));
httpClient.executeMethod(postMethod);
// checks server's status code first
int status = postMethod.getStatusCode();
System.out.println(status);
if (status == HttpURLConnection.HTTP_OK || status == HttpURLConnection.HTTP_CREATED) {
String jsonText = postMethod.getResponseBodyAsString();
JSONObject json = new JSONObject(jsonText);
System.out.println(jsonText);
} else {
throw new MyException(postMethod.getResponseBodyAsString());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
postMethod.releaseConnection();
}
答案 1 :(得分:0)
您是否验证了父ID = 11446498是否有效?如果您只是测试此端点,请尝试使用ID = 0代表根文件夹。
答案 2 :(得分:0)
BoxConfig boxConfig = BoxConfig.readFrom(new FileReader("box_config.json")); //.json configuration file can be downloaded from dev console based on your app settings
BoxAPIConnection api = BoxDeveloperEditionAPIConnection.
getAppUserConnection(USER_ID, boxConfig);
BoxFolder boxFolder = new BoxFolder(api, FOLDER_ID);
boxFolder.uploadFile(stream, filename);
请注意,仅创建应用程序并将其设置为可读写还不够,您还需要在管理控制台-企业设置中授权该应用程序。使用客户ID来授权新应用