BOX API 上传文件 Java

BOX API Upload a file Java

我尝试使用 Box API 将文件上传到 Box。

但是无论我尝试什么,我总是收到没有任何其他信息的 400 Bad Request。

对这个问题有什么想法吗?

API 中的示例是这个 curl 请求:

curl https://upload.box.com/api/2.0/files/content \
-H "Authorization: Bearer ACCESS_TOKEN" -X POST \
-F attributes='{"name":"tigers.jpeg", "parent":{"id":"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();
    }

您是否验证了父 ID = 11446498 是否有效?如果您只是测试此端点,请尝试使用代表根文件夹的 ID = 0。

我找到了解决方法,不同的部分不正确。 我必须创建 3 个部分:

  1. Parent_id : 父文件夹的id
  2. 元数据:json
  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();
    }
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);

请注意,仅仅创建应用程序并设置为可读写是不够的,您还需要在管理控制台-企业设置中对应用程序进行授权。使用 client Id 授权新应用