@GetMapping("/authorize")
public String authorize() {
return "redirect:" + tokenUrl + "?client_id=" + clientId +
"&redirect_uri=" + redirectUri +
"&response_type=code" +
"&scope=" + scope;
}
@GetMapping("/callback")
public String callback(@RequestParam("code") String code) {
try {
System.out.println("Entering callback method");
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("client_id", clientId);
map.add("client_secret", clientSecret);
map.add("code", code);
map.add("redirect_uri", redirectUri);
map.add("grant_type", "authorization_code");
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
String tokenurl = "https://login.microsoftonline.com/common/oauth2/v2.0/token";
ResponseEntity<String> response = restTemplate.postForEntity(tokenurl, request, String.class);
if (response.getStatusCode() == HttpStatus.OK) {
String jsonResponse = response.getBody();
ObjectMapper objectMapper = new ObjectMapper();
JsonNode root = objectMapper.readTree(jsonResponse);
System.out.println(jsonResponse);
accessToken = root.get("access_token").asText();
System.out.println("accesstoken as a response :"+accessToken);
return "redirect:/upload";
} else {
System.out.println("Error Occurs");
return "error";
}
} catch (Exception e) {
System.out.println("Error Occurs" + e);
return "error";
}
}
@PostMapping("/upload")
@ResponseBody
public String uploadFile(@RequestParam("file") MultipartFile file) {
try {
System.out.println("Headers Upload !!!");
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", "Bearer " + accessToken);
byte[] byte1 = file.getBytes();
HttpEntity<byte[]> requestEntity = new HttpEntity<>(byte1, headers);
//Error
System.out.println("\n"+requestEntity+"\n"+requestEntity.getBody()+"\n");
System.out.println(uploadUrl + "/" + folderId + "/items/" + file.getOriginalFilename() + ":/content");
ResponseEntity<String> response = restTemplate.exchange(
uploadUrl + "/" + file.getOriginalFilename() + ":/content",
HttpMethod.PUT,
requestEntity,
String.class);
System.out.println("Response : "+response.getBody());
if (response.getStatusCode() == HttpStatus.CREATED) {
return "File uploaded successfully!";
} else {
return "Something went wrong !!!";
}
} catch (Exception ex) {
System.out.println("Error Occurred " + ex.getMessage());
return "Something went wrong !!! ";
}
}
@PostMapping("/uploadf")
public String uploadfile1() {
try {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", "Bearer " + accessToken);
// Read file content and encode it as Base64
Path filePath = Paths.get("C:\\Users\\91638\\Documents\\Hello.docx");
byte[] fileContent = Files.readAllBytes(filePath);
String fileContentBase64 = Base64.getEncoder().encodeToString(fileContent);
// Create JSON payload with the file content
Map<String, String> requestBody = new HashMap<>();
requestBody.put("file_content", fileContentBase64);
// Serialize the map to JSON
ObjectMapper objectMapper = new ObjectMapper();
String requestBodyJson = objectMapper.writeValueAsString(requestBody);
// Create HttpEntity with the JSON payload
HttpEntity<String> requestEntity = new HttpEntity<>(requestBodyJson, headers);
// Send the request
ResponseEntity<String> response = restTemplate.exchange(
uploadUrl + "/Hello.docx:/content",
HttpMethod.PUT,
requestEntity,
String.class);
if (response.getStatusCode() == HttpStatus.CREATED) {
return "File uploaded successfully!";
} else {
return "Something went wrong!";
}
} catch (Exception ex) {
System.out.println("Error Occurred: " + ex.getMessage());
return "Something went wrong!";
}
}
@PostMapping("/upload1")
public ResponseEntity<String> uploadFile(@RequestBody byte[] fileBytes) {
try {
// Prepare the request headers
ObjectMapper objectMapper = new ObjectMapper();
String content = Base64.getEncoder().encodeToString(fileBytes);
Map<String, String> requestBody = Collections.singletonMap("content", content);
String jsonBody = objectMapper.writeValueAsString(requestBody);
System.out.println(jsonBody);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", "Bearer " + accessToken);
// Create a request entity with the file bytes and headers
HttpEntity<byte[]> requestEntity = new HttpEntity<>(fileBytes, headers);
// Prepare the upload URL
// Make the HTTP PUT request to upload the file
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.exchange(
uploadUrl + "/" + "person.csv" + ":/content",
HttpMethod.PUT,
requestEntity,
String.class);
if (response.getStatusCode() == HttpStatus.CREATED) {
return ResponseEntity.ok("File uploaded successfully!");
} else {
return ResponseEntity.status(response.getStatusCode()).body("Something went wrong: " + response.getStatusCode());
}
} catch (Exception ex) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error uploading file: " + ex.getMessage());
}
}
@PostMapping("/uploadj")
public ResponseEntity<String> uploadFile(@RequestBody String jsonPayload) {
try {
// Assuming the JSON payload contains the file content
byte[] fileBytes = jsonPayload.getBytes();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", "Bearer " + accessToken);
// Create a request entity with the file bytes and headers
HttpEntity<byte[]> requestEntity = new HttpEntity<>(fileBytes, headers);
// Prepare the upload URL (replace with your actual upload URL)
// Make the HTTP PUT request to upload the file
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.exchange(
uploadUrl + "/" + "person.txt" + ":/content",
HttpMethod.PUT,
requestEntity,
String.class);
if (response.getStatusCode() == HttpStatus.CREATED) {
return ResponseEntity.ok("File uploaded successfully!");
} else {
return ResponseEntity.status(response.getStatusCode()).body("Something went wrong: " + response.getStatusCode());
}
} catch (Exception ex) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error uploading file: " + ex.getMessage());
}
this is the code im working i tried whole different way to upload my file using postman but each mapping provide different errors
one provide entity only allows writes with a JSON Content-Type header.
if i provide the actual json data it throw bad argument error its so frustrating to upload my file kindly someone help me to get rid of this I use gpt, gemini, github, etc nothing helps