Google Spreadsheet API v4以“添加一行"在电子表格中使用"ApiKey" [英] Google Spreadsheet API v4 to "add a row" in spreadsheet using an "ApiKey"

查看:63
本文介绍了Google Spreadsheet API v4以“添加一行"在电子表格中使用"ApiKey"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Google Spreadsheet API v4在电子表格中直接使用"ApiKey"在添加行"而不使用OAuth 2.0.

I am trying to use Google Spreadsheet API v4 to "add a row" in spreadsheet using an "ApiKey" directly without using an OAuth 2.0.

如果我使用的是OAuth 2.0,则可以成功添加一行",但是我的要求是使用"ApiKey"

If I am using OAuth 2.0 its successfully "adding a row" but my requirement is to use "ApiKey"

引用了该URL: https://developers.google.com/sheets/api/guides/migration#add_a_new_row_of_data

但是没有使用"APIKey"的适当文档.当我尝试使用"Apikey"时,抛出了未经授权(401)"错误.

But there is no proper document for using "APIKey ". When I tried using "Apikey" it is throwing me an error of "Unauthorized (401)".

推荐答案

虽然文档尚不完全清楚,但可以在不经过应用必须征得同意的OAuth流程的情况下,向电子表格中添加行来自特定用户.

While the documentation isn't entirely clear, it is possible to add rows to a spreadsheet without needing to go through the OAuth flow where the app has to obtain consent from a specific user.

使用服务帐户时,该流程与使用API​​Key时的流程非常相似,不同之处在于可以明确授予服务帐户写入表的权限.

The flow, when using a service account, is very similar to the flow when using an APIKey, except a service account can be given explicit authorization to write to the sheet.

之所以可行,是因为服务帐户就像一个用户,只是一个非人类的用户.过程如下:

This works because a service account is like a user, just one that is not human. The process is as follows:

  • 转到Google Cloud Console,然后在您的GCP项目中创建一个服务帐户.(如果您没有项目,则可能需要创建一个项目.)
  • 创建密钥并将其下载为JSON文件.JSON文件将包含私钥,密钥ID,服务帐户的电子邮件地址以及其他信息.请将其保存在安全的地方.这是敏感数据.
  • 接下来,在电子表格中,以具有编辑访问权限的用户身份添加服务帐户的电子邮件地址.

然后,您现在可以使用服务帐户client_email和JSON文件中的private_key来创建JWT令牌来授权请求​​.由于服务帐户是工作表中的授权编辑者,因此您现在可以写入工作表中.

Afterwards, you can now use the service account client_email and the private_key from the JSON file in order to create a JWT token to authorize the request. You can now write to the sheet, since the service account is an authorized editor in the sheet.

有关更多信息,请参见使用OAuth 2.0访问Google API-服务帐户信息.请注意,尽管文档听起来像服务帐户无法访问用户数据,但在这种情况下,因为我们将服务帐户添加为工作表的编辑者,所以可以.

See Using OAuth 2.0 to Access Google APIs - Service Accounts for more information. Note that although the documentation makes it sound like the service account can't access user data, it can in this case since we add the service account as an editor on the sheet.

另外,请参见将OAuth 2.0用于服务器到服务器的应用程序.

现在,我要说的是,您的问题表明您需要使用API​​Key".相反,我假设您的要求是该应用程序必须能够在不提示用户进行身份验证的情况下写入工作表.如果是这种情况,那么就可以解决您的问题,但是如果问题确实涉及使用API​​Key,那么唯一的方法是使工作表对于所有未经身份验证的用户都是可公开写的.

Now, I will say that your question states that you "require using an APIKey". Instead, I'm assuming that your requirement is that the app must be able to write to the sheet without prompting users for authentication. If that's the case, then this solves your problem, but if the question really involves using an APIKey, then the only way to do that is to make the sheet publicly writeable to all unauthenticated users.

我也不知道您使用的是哪种编程语言,因此我将创建JWT令牌留给读者作为练习,并且链接的文档还描述了Java,Python和Java的过程.原始HTTP/REST请求.希望这会有所帮助!

I also don't know what programming language you're using, so I'll leave the creation of a JWT token as an exercise for the reader, and the linked documentation also describes the process in Java, Python, and as a raw HTTP/REST request. Hope this helps!

这篇关于Google Spreadsheet API v4以“添加一行"在电子表格中使用"ApiKey"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆