3

我正嘗試使用POST REST調用將文件上載到OneDrive文件夾中。 我的應用程序能夠與OneDrive進行通信。我得到的迴應是The request entity body isn't a valid JSON object.Onedrive API文件使用Java上傳

下面是我的代碼,請讓我知道代碼的錯誤部分或我的方法。

public static void onedriveFileUpload() { 
     CloseableHttpClient httpClient = HttpClients.createDefault(); 
     HttpPost uploadFile = new HttpPost("https://apis.live.net/v5.0/folder.id"); 

     MultipartEntityBuilder builder = MultipartEntityBuilder.create(); 
     uploadFile.addHeader("Authorization", "Bearer access_token"); 
     builder.addPart("file", new FileBody(new File("Test.txt"), ContentType.APPLICATION_OCTET_STREAM, "Test.txt")); 

     builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); 
     Charset chars = Charset.forName("utf-8"); 
     builder.setCharset(chars); 
     uploadFile.setEntity(builder.build()); 
     uploadFile.setHeader("Content-Type", "multipart/form-data"); 
     uploadFile.setHeader("charset", "UTF-8"); 
     uploadFile.setHeader("boundary", "AaB03x"); 
     HttpResponse response = null; 
    try { 
     response = httpClient.execute(uploadFile); 
     HttpEntity responseEntity = response.getEntity(); 
     String json = EntityUtils.toString(responseEntity); 
     System.out.println(json); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    } 

這是我從OneDrive獲得的Json響應。

{ 
    "error": { 
     "code": "request_body_invalid", 
     "message": "The request entity body isn't a valid JSON object." 
    } 
} 
+0

像這樣的錯誤最好用Fiddler調試,http://www.telerik.com/fiddler。在不知道請求內容的情況下,只會得到發送請求的錯誤。 – 2014-09-22 19:14:34

回答

0

如果要使用POST方法,則需要POST到/api.live.net/v5.0/folder.id/files。您的代碼在文件夾ID後缺少/files部分。如果您仍然遇到麻煩,請顯示實際HTTP請求的樣子會有所幫助。

0

您應該發佈https://apis.live.net/v5.0/:albumId/files/:fileName[.:format]。而不是MultipartEntity,請嘗試使用ByteArrayEntity。示例:

public Photo uploadPhoto(String accessToken, String albumId, String format, byte[] bytes) throws IOException { 
    Photo newPhoto = null; 
    URI uri; 
    try { 
     uri = new URIBuilder().setScheme(DEFAULT_SCHEME) 
           .setHost(API_HOST) 
           .setPath("/" + albumId + "/files/" + 
             UUID.randomUUID().toString() + "." + format) 
           .addParameter("access_token", accessToken) 
           .addParameter("downsize_photo_uploads", "false") 
           .build(); 
    } catch (URISyntaxException e) { 
     e.printStackTrace(); 
     throw new IllegalStateException("Invalid album path"); 
    } 

    CloseableHttpClient httpClient = HttpClients.createDefault(); 
    try { 
     HttpPut httpPut = new HttpPut(uri); 
     ByteArrayEntity imageEntity = new ByteArrayEntity(bytes); 
     httpPut.setEntity(imageEntity); 
     Map<Object, Object> rawResponse = httpClient.execute(httpPut, new SomeCustomResponseHandler()); 
     if (rawResponse != null) { 
      newPhoto = new Photo(); 
      newPhoto.setName((String) rawResponse.get("name")); 
      newPhoto.setId((String) rawResponse.get("id")); 
      // TODO:: Do something else. 
     } 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     httpClient.close(); 
    } 

    return newPhoto; 
} 

該代碼是OneDrive4J的一個片段。查看https://github.com/nicknux/onedrive4j。今年早些時候,當我試圖尋找OneDrive Java客戶端時,我建立了它。