使用服务帐户在 NodeJS 上设置 Google Drive API
Setting up Google Drive API on NodeJS using a service account
我正在尝试使用服务帐户通过 NodeJS 服务器连接到 Google 驱动器 API。目标是让服务器能够作为服务帐户进行身份验证,从驱动器中检索相关文件,并将它们发送回用户,而无需用户直接登录 Google。这将允许我通过我的网络应用程序控制文件访问,而不必通过 Drive 手动共享和取消共享文件。以我对Google驱动API的理解,这应该都是可以的。问题是我什至不知道如何验证我的服务器。 AWS EC2 实例上的服务器 运行s。澄清一下,我不希望用户必须使用前端界面进行身份验证。
我遵循了 quickstart guide and set up a service account & key as instructed here,但是在按照第二个 link 中的说明创建密钥后,我似乎没有正确的 credentials.json
文件。在 Google Developer Console 上生成密钥后得到的 JSON 文件具有以下对象键(值有意删除):
type
、project_id
、private_key_id
、private_key
、client_email
、client_id
、auth_uri
、token_uri
、auth_provider_x509_cert_url
、client_x509_cert_url
快速入门指南建议此文件应在某些 installed
对象 (const {client_secret, client_id, redirect_uris} = credentials.installed;
) 中包含 client_secret
和 redirect_uris
:
尝试 运行 这个 index.js
快速启动文件会导致抛出错误,因为 installed
在 credentials.json
中不存在。我在哪里可以生成必要的凭据文件?还是我完全走错了路?
像 这样的帖子在旧版本的快速入门文档中提到了类似的问题,但这里的解决方案没有帮助,因为我的凭据文件中没有 client_secret
密钥。
当我看到您的 credentials.json
文件的显示密钥时,我了解到该文件是服务帐户的凭据文件。如果我的理解是正确的,当我看到你的展示脚本时,该脚本似乎是针对 OAuth2 的。在这种情况下,此脚本不能用于服务帐户。我认为这就是您当前问题的原因。
为了使用服务帐户使用云端硬盘 API,下面的示例脚本怎么样?
示例脚本:
在使用此脚本之前,请设置服务帐户的credentialFilename
。在这种情况下,请包括路径。
const { google } = require("googleapis");
const credentialFilename = "credentials.json";
const scopes = ["https://www.googleapis.com/auth/drive.metadata.readonly"];
const auth = new google.auth.GoogleAuth({keyFile: credentialFilename, scopes: scopes});
const drive = google.drive({ version: "v3", auth });
// This is a simple sample script for retrieving the file list.
drive.files.list(
{
pageSize: 10,
fields: "nextPageToken, files(id, name)",
},
(err, res) => {
if (err) return console.log("The API returned an error: " + err);
const files = res.data.files;
console.log(files);
}
);
- 当此脚本为 运行 时,作为示例脚本,文件列表是从服务帐户的 Google 驱动器中检索的。所以,请根据您的实际情况进行修改。
- 此示例脚本使用
https://www.googleapis.com/auth/drive.metadata.readonly
作为范围。请根据实际情况修改。
参考:
我正在尝试使用服务帐户通过 NodeJS 服务器连接到 Google 驱动器 API。目标是让服务器能够作为服务帐户进行身份验证,从驱动器中检索相关文件,并将它们发送回用户,而无需用户直接登录 Google。这将允许我通过我的网络应用程序控制文件访问,而不必通过 Drive 手动共享和取消共享文件。以我对Google驱动API的理解,这应该都是可以的。问题是我什至不知道如何验证我的服务器。 AWS EC2 实例上的服务器 运行s。澄清一下,我不希望用户必须使用前端界面进行身份验证。
我遵循了 quickstart guide and set up a service account & key as instructed here,但是在按照第二个 link 中的说明创建密钥后,我似乎没有正确的 credentials.json
文件。在 Google Developer Console 上生成密钥后得到的 JSON 文件具有以下对象键(值有意删除):
type
、project_id
、private_key_id
、private_key
、client_email
、client_id
、auth_uri
、token_uri
、auth_provider_x509_cert_url
、client_x509_cert_url
快速入门指南建议此文件应在某些 installed
对象 (const {client_secret, client_id, redirect_uris} = credentials.installed;
) 中包含 client_secret
和 redirect_uris
:
尝试 运行 这个 index.js
快速启动文件会导致抛出错误,因为 installed
在 credentials.json
中不存在。我在哪里可以生成必要的凭据文件?还是我完全走错了路?
像 client_secret
密钥。
当我看到您的 credentials.json
文件的显示密钥时,我了解到该文件是服务帐户的凭据文件。如果我的理解是正确的,当我看到你的展示脚本时,该脚本似乎是针对 OAuth2 的。在这种情况下,此脚本不能用于服务帐户。我认为这就是您当前问题的原因。
为了使用服务帐户使用云端硬盘 API,下面的示例脚本怎么样?
示例脚本:
在使用此脚本之前,请设置服务帐户的credentialFilename
。在这种情况下,请包括路径。
const { google } = require("googleapis");
const credentialFilename = "credentials.json";
const scopes = ["https://www.googleapis.com/auth/drive.metadata.readonly"];
const auth = new google.auth.GoogleAuth({keyFile: credentialFilename, scopes: scopes});
const drive = google.drive({ version: "v3", auth });
// This is a simple sample script for retrieving the file list.
drive.files.list(
{
pageSize: 10,
fields: "nextPageToken, files(id, name)",
},
(err, res) => {
if (err) return console.log("The API returned an error: " + err);
const files = res.data.files;
console.log(files);
}
);
- 当此脚本为 运行 时,作为示例脚本,文件列表是从服务帐户的 Google 驱动器中检索的。所以,请根据您的实际情况进行修改。
- 此示例脚本使用
https://www.googleapis.com/auth/drive.metadata.readonly
作为范围。请根据实际情况修改。