如何使用 Nodejs 在没有 OAuth 的情况下使用 Google Drive API?
在搜索 stackoverflow、博客、YouTube、Google Drive API 文档等后,大多数示例都展示了如何通过 OAuth 使用 Drive API。
我想构建一个 Nodejs 应用程序,只有当新用户在我的应用程序中创建帐户时,Nodejs 服务器才会在 Google Drive 上创建电子表格。然后该电子表格将可供应用程序管理员使用。
这是一个服务器端进程,因此不需要 OAuth 同意屏幕等。
是否没有办法仅通过 API 密钥和 REST URL 使用 Drive API?
下面的 Google 文档链接有仅使用 REST URL 与驱动API。
https://developers.google.com/drive/api/v3/ Reference/files/create
Google 文档对于如何仅使用上面链接中的 API 密钥和 REST URL 使用 Drive API 的内容含糊其辞,因为示例使用 REST URL 创建文件等
After searching stackoverflow, blogs, YouTube, Google Drive API doc, e.t.c, most of the examples show how to use the Drive API using OAuth.
I want to build a Nodejs application where the Nodejs Server will create Spreadsheets on Google Drive only when new users create an account in my application. The spreadsheet will then be available to the application admin.
This is a server side process so there is no need for the OAuth Consent screen e.t.c
Is there not a way to use the Drive API with just API keys and REST URL’s
The Google Doc link below has examples of using just REST URL’s to interact with the Drive API.
https://developers.google.com/drive/api/v3/reference/files/create
The Google Docs is vague on how to use the Drive API with just API keys and REST URL’s in the link above as the examples use REST URL’s to create files e.t.c
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先您需要了解私有数据和公共数据之间的区别。
公共数据是不为任何人所拥有的数据。一个很好的例子是公共 YouTube 视频。您不需要所有者的许可即可查看此视频,您可以使用公共 api 密钥访问搜索视频列表方法,并且您将能够访问它们。
私有数据是用户拥有的数据。您的谷歌云端硬盘帐户是私人用户数据。只有您可以访问它。只有您可以授予应用程序访问它的权限。
公共 api 密钥只能用于访问公共数据。要访问私人用户数据,您需要获得所有者的同意
您想要使用 file.create 方法,如果您检查文档,您会发现它准确地告诉您这一点。
您需要获得授权,并且用户必须同意至少其中一个范围,您才能使用该方法。
因此,要回答您的问题
是否没有办法仅通过 API 密钥和 REST URL 使用 Drive API
,答案是否定的。不使用 api 密钥。不过我还有一个选择。您的应用程序将连接到开发人员控制的帐户。这意味着您可以使用服务帐户。服务帐户就像虚拟用户,如果配置正确,您可以通过与服务帐户共享文件夹来授予其对驱动器帐户上文件夹的访问权限,就像与任何其他用户一样。完成此操作后,服务帐户将能够从您的服务器访问该驱动器帐户,而无需您进行其他交互。
代码无耻地从 使用 Node Js 将图像上传到 Google 驱动器复制
The first thing you need to understand the diffrence between private data and public data.
Public data is data that is not owned by anyone. A good example would be Public youtube videos. You do not need the permission of the owner to see thise videos you can use a Public api key to access the search videos list method and you will be able to access them.
Private data is data that is owned by a user. Your google drive account is private user data. Only you can access that. Only you can grant an application permission to access it.
Public api keys can only bue used to access public data. To access private user data you need the consent of the owner
You want to use the file.create method if you check the documentation you will find that it tells you exactly that.
You need to be authorized and the user must have consented to at least one of those scopes for you to be able to use that method.
So to answer your question
Is there not a way to use the Drive API with just API keys and REST URL’s
the answer is No there is not. Not with an api key.However I have an alternative. Your application will be connecting to an account you the developer control. This means that you can use a service account. Service accounts are like dummy users if configured properly you can grant it access to a folder on your drive account by sharing that folder with the service account like you would any other user. Once that is done the service account will be able to access that drive account from your server with no other interaction on your part.
Code shamelessly copied from Upload Image to Google drive with Node Js