0

I try to upload an excel in OneDrive in Java. But I hava an 400 Bad Request error.

Here is my code:

File currDir = new File(".");
String path = currDir.getAbsolutePath();
String filename ="V2_POS_INV_AMPLIFY_RS70703_"+start+"_"+end + ".xlsx";
String fileLocation = path + "\\"+filename;
String urlFile = "https://graph.microsoft.com/v1.0/users/m.gomes@megahertz.ch/drive/items/" + drive.getId() + ":/" + filename + ":/content";
URL fileUrl = new URL(urlFile);
HttpURLConnection con = (HttpURLConnection) fileUrl.openConnection();
con.setRequestProperty("content-type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
con.setRequestProperty("Authorization", "Bearer "+ token.getAccess_token());
con.setRequestMethod("PUT");
con.setDoOutput(true);
con.setDoInput(true);
workbook.write(con.getOutputStream());
workbook.close();

Here is the error that I have: 400: Bad Request

{
   "error": {
      "code":"invalidRequest",
      "message":"Invalid request",
      "innerError": {
         "date":"2022-01-13T15:28:26",
         "request-id":"10a96485-9ab6-43b8-b3b1-d8de437c65e8",
         "client-request-id":"10a96485-9ab6-43b8-b3b1-d8de437c65e8"
      }
   }
}

When I test the request on Postman, it's working perfectly.

test on postman

postman Headers

Is there anybody that can help me ?

Elikill58
  • 4,050
  • 24
  • 23
  • 45

0 Answers0