简体   繁体   中英

Error while uploading file on google drive V3 by RestTemplates

I am using Google Drive v3 and trying to upload the file using RestTemplate, but I get an following error:-

org.springframework.web.client.HttpClientErrorException: 400 Bad Request
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:700)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:653)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:531)
    at com.bostonbyte.thelift.utils.GoogleDriveUtil.uploadFileByRest(GoogleDriveUtil.java:411)
    at com.bostonbyte.thelift.utils.GoogleDriveDown.main(GoogleDriveDown.java:19)

2017-09-27 11:37:27 [main] ERROR c.b.thelift.utils.GoogleDriveUtil -
                Exception in getLatestFileChanges method of org.springframework.web.client.HttpClientErrorException
org.springframework.web.client.HttpClientErrorException: 400 Bad Request
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:700)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:653)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:531)
    at com.bostonbyte.thelift.utils.GoogleDriveUtil.uploadFileByRest(GoogleDriveUtil.java:411)
    at com.bostonbyte.thelift.utils.GoogleDriveDown.main(GoogleDriveDown.java:19)

My FileUploading Method:

static void uploadFileByRest() {
        try {
            String accessToken = GoogleDriveCredentialsProvider.getAccessToken();

            RestTemplate restTemplate = new RestTemplate();
            HttpHeaders httpHeaders = new HttpHeaders();
            LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
            map.add("file", new ClassPathResource("demo.txt"));
            map.add("name", "demo");

            httpHeaders.setContentType(org.springframework.http.MediaType.MULTIPART_FORM_DATA);
            httpHeaders.add(Constant.Header.AUTH_TOKEN, BEARER + accessToken);
            HttpEntity<String> entity = new HttpEntity<>(httpHeaders);
            HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new    HttpEntity<LinkedMultiValueMap<String, Object>>(
                    map, httpHeaders);
            ResponseEntity<String> responseEntity;
            responseEntity = restTemplate.exchange(Constant.Google.UPLOAD_FILE, HttpMethod.POST, requestEntity,
                    String.class);

            ObjectMapper objectMapper = new ObjectMapper();
        System.out.println(""+responseEntity.getBody());




        } catch (Exception e) {
            LOGGER.error("Exception in getLatestFileChanges method of " + e.getClass().getName(), e);
        }

    }

Following URL I am using for upload the File:

" https://www.googleapis.com/drive/v3/files?uploadType=multipart ";

When i call this method with drive as parameter that time i get that error , please help me.Thanks in advance

I got the answer of my question, following method update the file on google drive by using resttemplates,

public static void updateFile() {
    Drive drive = getService();
    File fileMetadata = new File();
    fileMetadata.setName("Harshad");
    fileMetadata.setMimeType("application/vnd.google-apps.spreadsheet");
    java.io.File filePath = new java.io.File("/home/xpointers/convertcsv.csv");

    FileContent mediaContent = new FileContent("text/csv", filePath);
    // Send the request to the API.
    try {
        File updatedFile = drive.files().update(fileId, fileMetadata, mediaContent).execute();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM