【问题标题】:Error while uploading file on google drive V3 by RestTemplatesRestTemplate 在谷歌驱动器 V3 上上传文件时出错
【发布时间】:2017-09-27 06:15:57
【问题描述】:

我正在使用 Google Drive v3 并尝试使用 RestTemplate 上传文件,但出现以下错误:-

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)

我的文件上传方法:

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);
        }

    }

以下网址我用于上传文件:

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

当我使用驱动器作为参数调用此方法时,我得到了那个错误,请帮助我。提前致谢

【问题讨论】:

  • 请再次确认您显示的https://www.googleapis.com/drive/v3/files?uploadType=multipart的端点。如果你真的用这个,修改成https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart怎么样?在多部分请求中,https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart 用作端点。您可以在here查看详细信息。如果此信息对您没有用处,我很抱歉。

标签: java google-drive-api resttemplate


【解决方案1】:

我得到了我的问题的答案,以下方法使用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();
    }
}

【讨论】:

  • 此方法使用分段上传?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-22
  • 1970-01-01
  • 1970-01-01
  • 2019-08-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多