2016-05-24 11 views
1

Box APIを使用してファイルをBoxにアップロードしようとしています。BOX APIファイルをアップロードするJava

私は何をしようと、私はいつも他の情報なしで400悪い要求を受け取ります。

問題についてご意見はありますか?

APIの例では、このカール要求である:

カールhttps://upload.box.com/api/2.0/files/content
\ -H "許可:ベアラaccess_tokenは" = "{ "名前" 属性-F -X POST \
:」 tigers.jpeg」、 "親":{ "ID": "11446498"}}」\
-F [email protected]

私のコードは、以下である:

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

親ID = 11446498が有効であることを確認しましたか?このエンドポイントをテストするだけの場合は、ルートフォルダを表すID = 0を試してみてください。

+0

親ID「11446498」は、APIで使用されている例です。私のコードで使用しているものが有効であることを確認しました。 – Neptune

+1

URLエンドポイントから末尾の「/」を削除してみます。正しいエンドポイントは、[documentation](https://upload.box.com/api/2.0/files/content](https://docs.box.com/reference#upload-a-file) //docs.box.com/reference#upload-a-file)。 コードの最初の行は、 'String URL =" https://upload.box.com/api/2.0/files/content "; ' – Brent

+0

ありがとう、ブレント、私の最大の問題でしたコード! – Neptune

0

私は解決策を見つけましたが、さまざまな部分が正しくありませんでした。

  1. PARENT_ID: 私は3部を作成する必要がありましたJSON
  2. ファイル:
  3. は、メタデータの親フォルダのIDファイルをアップロードするに

このコードは動作します:

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(); 
    } 
関連する問題