如何在Java中从Google Drive API获取Gdoc内容?
我正试图用谷歌驱动API获取谷歌文档的内容。我成功获取了文件夹并获得了Google Docs文件id 在API文档中,据说我可以下载如下文件:如何在Java中从Google Drive API获取Gdoc内容?,java,google-api,google-drive-api,google-docs-api,google-api-java-client,Java,Google Api,Google Drive Api,Google Docs Api,Google Api Java Client,我正试图用谷歌驱动API获取谷歌文档的内容。我成功获取了文件夹并获得了Google Docs文件id 在API文档中,据说我可以下载如下文件: 私有字符串getGdoc(字符串id)引发异常{ if(service==null)抛出新异常(); OutputStream OutputStream=新建ByteArrayOutputStream(); service.files().export(id,“text/plain”) .ExecuteMedia和DownloadTo(输出流); 返回n
私有字符串getGdoc(字符串id)引发异常{
if(service==null)抛出新异常();
OutputStream OutputStream=新建ByteArrayOutputStream();
service.files().export(id,“text/plain”)
.ExecuteMedia和DownloadTo(输出流);
返回null;
}
我的问题是,我不想将其写入带有OutputStream
的文件中,但我想将文件内容写入字符串中
因此我尝试使用输入流
:
私有字符串getGdoc(字符串id)引发异常{
if(service==null)抛出新异常();
InputStream InputStream=service.files().export(id,“文本/普通”)
.ExecuteMediaaInputStream();
System.out.println(IOUtils.toString(inputStream,StandardCharsets.UTF_8));
返回null;
}
通过InputStream
我获得了文件的内容,但是我的文档中有一些日文字符,它们没有显示出来。我只在控制台中获得这些日文字符的?
。我不知道它来自哪里,因为我指定使用UTF-8字符集
当我尝试使用OutputStream
写入txt文件时,我没有任何无法识别字符的问题
我不知道实现这一目标的最佳方式是什么。你能帮我吗?谷歌硬盘api是一种文件存储api。它可以让你上传和下载谷歌硬盘中存储的文件。下载文件时,您可以将其保存到计算机中,然后在第三方应用程序中打开
如果要检查和操作存储在google drive上的google文档文件的内容。你应该考虑使用
import com.google.api.client.auth.oauth2.Credential;
导入com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
导入com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
导入com.google.api.client.googleapis.auth.oauth2.googleaauthorizationcodeflow;
导入com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
导入com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
导入com.google.api.client.http.javanet.NetHttpTransport;
导入com.google.api.client.json.JsonFactory;
导入com.google.api.client.json.jackson2.JacksonFactory;
导入com.google.api.client.util.store.FileDataStoreFactory;
导入com.google.api.services.docs.v1.docs;
导入com.google.api.services.docs.v1.DocsScopes;
导入com.google.api.services.docs.v1.model.Document;
导入java.io.FileNotFoundException;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.security.GeneralSecurityException;
导入java.util.Collections;
导入java.util.List;
公共类DocsQuickstart{
私有静态最终字符串应用程序\u NAME=“Google Docs API Java Quickstart”;
私有静态最终JsonFactory JSON_FACTORY=JacksonFactory.getDefaultInstance();
私有静态最终字符串令牌\u目录\u路径=“令牌”;
私有静态最终字符串文档\u ID=“195j9eDD3ccgjQRttHhJPymLJUCOUjs jmwTrekvdjFE”;
/**
*此快速启动所需作用域的全局实例。
*如果修改这些作用域,请删除以前保存的令牌/文件夹。
*/
私有静态最终列表范围=Collections.singletonList(DocsScopes.DOCUMENTS\u READONLY);
私有静态最终字符串凭据文件路径=“/CREDENTIALS.json”;
/**
*创建授权凭据对象。
*@param HTTP_传输网络HTTP传输。
*@返回授权凭证对象。
*@在找不到credentials.json文件时引发IOException。
*/
私有静态凭据getCredentials(最终NetHttpTransport HTTP_传输)引发IOException{
//加载客户端机密。
InputStream in=DocsQuickstart.class.getResourceAsStream(凭证文件路径);
if(in==null){
抛出新的FileNotFoundException(“未找到资源:+凭证\文件\路径”);
}
GoogleClientSecrets clientSecrets=GoogleClientSecrets.load(JSON_工厂,新的InputStreamReader(in));
//生成流并触发用户授权请求。
GoogleAuthorizationCodeFlow=新建GoogleAuthorizationCodeFlow.Builder(
HTTP_传输、JSON_工厂、客户端机密、作用域)
.setDataStoreFactory(新的FileDataStoreFactory(新的java.io.File(令牌\目录\路径)))
.setAccessType(“脱机”)
.build();
LocalServerReceiver=新的LocalServerReceiver.Builder().setPort(8888.build();
返回新的AuthorizationCodeInstalledApp(流程、接收方)。授权(“用户”);
}
publicstaticvoidmain(字符串…参数)抛出IOException、GeneralSecurityException{
//构建新的授权API客户端服务。
最终NetHttpTransport HTTP_TRANSPORT=GoogleNetHttpTransport.newTrustedTransport();
Docs service=new Docs.Builder(HTTP_传输、JSON_工厂、getCredentials(HTTP_传输))
.setApplicationName(应用程序名称)
.build();
//打印所请求文档的标题:
// https://docs.google.com/document/d/195j9eDD3ccgjQRttHhJPymLJUCOUjs-jmwTrekvdjFE/edit
文档响应=service.documents().get(Document_ID).execute();
String title=response.getTitle();
System.out.printf(“文档的标题为:%s\n”,标题);
}
}
很可能您的字符串包含正确的数据,并且c
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.docs.v1.Docs;
import com.google.api.services.docs.v1.DocsScopes;
import com.google.api.services.docs.v1.model.Document;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.GeneralSecurityException;
import java.util.Collections;
import java.util.List;
public class DocsQuickstart {
private static final String APPLICATION_NAME = "Google Docs API Java Quickstart";
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static final String TOKENS_DIRECTORY_PATH = "tokens";
private static final String DOCUMENT_ID = "195j9eDD3ccgjQRttHhJPymLJUCOUjs-jmwTrekvdjFE";
/**
* Global instance of the scopes required by this quickstart.
* If modifying these scopes, delete your previously saved tokens/ folder.
*/
private static final List<String> SCOPES = Collections.singletonList(DocsScopes.DOCUMENTS_READONLY);
private static final String CREDENTIALS_FILE_PATH = "/credentials.json";
/**
* Creates an authorized Credential object.
* @param HTTP_TRANSPORT The network HTTP Transport.
* @return An authorized Credential object.
* @throws IOException If the credentials.json file cannot be found.
*/
private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
// Load client secrets.
InputStream in = DocsQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
if (in == null) {
throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
}
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
.setAccessType("offline")
.build();
LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
}
public static void main(String... args) throws IOException, GeneralSecurityException {
// Build a new authorized API client service.
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
Docs service = new Docs.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
.setApplicationName(APPLICATION_NAME)
.build();
// Prints the title of the requested doc:
// https://docs.google.com/document/d/195j9eDD3ccgjQRttHhJPymLJUCOUjs-jmwTrekvdjFE/edit
Document response = service.documents().get(DOCUMENT_ID).execute();
String title = response.getTitle();
System.out.printf("The title of the doc is: %s\n", title);
}
}