1 回答
![?](http://img1.sycdn.imooc.com/54584cde0001d19202200220-100-100.jpg)
TA贡献1795条经验 获得超7个赞
您希望使用带有 Node.js 的 googleapis 将 Google 文档发布到 Web。
您要检索已发布的 URL。
您希望使用服务帐户来实现此目的。
您已经拥有服务帐户并能够使用 Drive API。
从您的问题和回复评论中,我可以理解为这样。如果我的理解是正确的,这个答案怎么样?请认为这只是几个可能的答案之一。
用法:
为了使用以下示例脚本,请执行以下流程。
准备一份谷歌文档。
在这种情况下,作为测试用例,请在您的 Google Drive 中创建新的 Google Document。
将创建的 Google 文档与作为作者的服务帐户的电子邮件共享。
检索 Google 文档的文件 ID。
将变量设置为以下示例脚本。
运行脚本。
在这种情况下,使用 Drive API 的“Revisions: update”。
这样,Google 文档就会发布到网络上,您可以在控制台中看到发布的 URL。
示例脚本:
const { google } = require("googleapis");
// Please set the email and private key of service account.
const auth = new google.auth.JWT(
"### email of service account ###",
null,
"### private key of service account ###" ,
["https://www.googleapis.com/auth/drive"]
);
const fileId = "###"; // Please set the file ID.
const drive = google.drive({ version: "v3", auth });
const body = {
resource: {
published: true,
publishedOutsideDomain: true,
publishAuto: true
},
fileId: fileId,
revisionId: 1
};
drive.revisions.update(body, err => {
if (err) {
console.error(err);
return;
}
console.log(`https://docs.google.com/document/d/${fileId}/pub`);
});
请将私钥设置为"-----BEGIN PRIVATE KEY-----\n###\n-----END PRIVATE KEY-----\n".
笔记:
发布的网址是https://docs.google.com/document/d/${fileId}/pub。在这种情况下,使用文件 ID。因为不幸的是,在当前阶段,https://docs.google.com/document/d/e/2PACX-###/pubhtmlGoogle API 无法检索到类似的 URL。
添加回答
举报