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();
}
親ID「11446498」は、APIで使用されている例です。私のコードで使用しているものが有効であることを確認しました。 – Neptune
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
ありがとう、ブレント、私の最大の問題でしたコード! – Neptune