首页 > 解决方案 > 如何使用顶点在谷歌驱动器的谷歌文档中添加正文(内容)

问题描述

我们想在谷歌驱动器中创建一个谷歌文档。并想将数据添加到谷歌文档中。我们现在正在驱动器中创建文件,但未添加文档的正文(内容)。

下面是用于创建文件的代码

public static void createDocument(String folderId, String documentName) {
    HttpRequest req = new HttpRequest();
    req.setMethod('POST');
    req.setEndpoint('https://www.googleapis.com/drive/v2/files');
    req.setHeader('Authorization', 'Bearer '+accessToken);
    req.setHeader('content-type', 'application/json');
    req.setTimeout(60*1000);
    String body = '{"title":"'+ documentName +'.txt","parents":[{"id":"'+ folderId +'"}],"body": {"content": "Text of document"}}';
    req.setBody(body);
    Http http = new Http();
    HttpResponse res = http.send(req);
}

所以你能帮助我们吗?

标签: restgoogle-drive-apigoogle-docsapexgoogle-docs-api

解决方案


我们想在谷歌驱动器中创建一个谷歌文档。

要在 Google 驱动器中创建文档,您应该使用 Google 驱动器 API v3创建方法文件创建分为两部分,元数据和文件流。您似乎正在尝试将文件的内容添加为元数据的一部分。它不是元数据的一部分,它必须作为文件流上传。 管理上传

并想将数据添加到谷歌文档中。

您需要了解 Google drive api 是一个文件存储 api,只是它无法写入文件本身。为此,您应该使用 Google Documents api,它允许您写入 google doc 文件。

我们现在正在驱动器中创建文件,但未添加文档的正文(内容)。

您还在使用 google drive v2,您应该考虑切换到 google drive v3。


推荐阅读