如何使用Java

时间:2018-08-17 11:12:01

标签: java spring-boot google-drive-api

    try {
        log.info(contentType + " " + title + " " + parentId);

        com.google.api.services.drive.model.File body = new com.google.api.services.drive.model.File();

        body.setName(title);    
        body.setMimeType("application/vnd.google-apps.document");
        List<String> parents = Arrays.asList(parentId);          
                        body.setParents(parents);
    com.google.api.client.http.AbstractInputStreamContent in1 = new AbstractInputStreamContent(
                contentType) {

            @Override
            public boolean retrySupported() {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public long getLength() throws IOException {
                // TODO Auto-generated method stub
                return inputStream.available();
            }

            @Override
            public InputStream getInputStream() throws IOException {
                // TODO Auto-generated method stub
                return inputStream;
            }
        };

        com.google.api.services.drive.model.File file = getDriveClient()
                .files().create(body, in1).setFields("id, webContentLink, webViewLink, parents").execute();
        System.out.println("File ID:::::::::::::: " + file.getId());

        return file;
    } catch (Exception exp) {
        exp.printStackTrace();
    }
    return null;
}

当我使用这种模仿类型时,出现错误

at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:146)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:432)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)

我正在使用googledrive V3 Api,我想在google docs中打开文件。所以,我正在使用此mimetype.If你想参考此URL https://developers.google.com/drive/api/v3/mime-types 请提示我该错误或任何代码的原因。

0 个答案:

没有答案