无需身份验证令牌即可访问 Google 电子表格 API [英] Access Google spreadsheet API without auth token
问题描述
我已经创建了 Google 电子表格,并授予了所有人的编辑权限(即使没有登录也可以进行编辑).
这是
可以在没有 OAuth
或 API Keys
的情况下写入电子表格.您需要使用 Service Account Keys
.
这是我为我的 Node.js 环境所做的.
- 从 https://console.cloud.google.com/apis/获取服务帐户密钥凭据(您还可以在此处限制允许此密钥执行的操作)
- 创建时,请务必点击
提供新的私钥
- 在询问您如何下载密钥时选择
JSON
.
- 创建时,请务必点击
- 您刚刚生成的
服务帐户密钥
包括一个client_email
.- 转到您的 google 电子表格并允许此
client_email
对该文档具有写入权限
- 转到您的 google 电子表格并允许此
使用以下代码进行身份验证
<代码>让 jwtClient = new google.auth.JWT(client_email, null, private_key, ["https://www.googleapis.com/auth/spreadsheets",]);//验证请求jwtClient.authorize(function(err, tokens) {//此时身份验证完成,您现在可以使用 `jwtClient`//读取或写入电子表格});
client_email
和 private_key
是 service account key
可以在此处找到更详细的说明.http://isd-soft.com/tech_blog/accessing-google-apis-using-service-account-node-js/ 此外,所有功劳都归于此页面.
I have created Google Spreadsheet, and given edit access to all (can edit even without login).
Here is the link. I would like to update this sheet with Google Spreadsheet API. But I am getting error. My requirement is update the sheet thru API even without access credential.
It is possible to write to spreadsheet without OAuth
or API Keys
. You need to use Service Account Keys
.
Here is what I did for my Node.js environment.
- Get a service account key from https://console.cloud.google.com/apis/credentials (You can here also restrict what this keys is allowed todo)
- When creating, make sure you click the
Furnish a new private key
- Select
JSON
when it asks you how to download the key.
- When creating, make sure you click the
- The
service account key
you have just generated includes aclient_email
.- Go to you google spreadsheet and allow this
client_email
to have write access on this document
- Go to you google spreadsheet and allow this
Use the following code to authenticate
let jwtClient = new google.auth.JWT(client_email, null, private_key, [ "https://www.googleapis.com/auth/spreadsheets", ]); //authenticate request jwtClient.authorize(function(err, tokens) { // at this point the authentication is done you can now use `jwtClient` // to read or write to the spreadsheet });
client_email
and private_key
are part of the service account key
A more detailed description can be found here. http://isd-soft.com/tech_blog/accessing-google-apis-using-service-account-node-js/ Also, all credit goes to this page.
这篇关于无需身份验证令牌即可访问 Google 电子表格 API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!