无需身份验证令牌即可访问 Google 电子表格 API [英] Access Google spreadsheet API without auth token

本文介绍了无需身份验证令牌即可访问 Google 电子表格 API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了 Google 电子表格,并授予了所有人的编辑权限(即使没有登录也可以进行编辑).

这是

解决方案

可以在没有 OAuthAPI Keys 的情况下写入电子表格.您需要使用 Service Account Keys.

这是我为我的 Node.js 环境所做的.

  1. https://console.cloud.google.com/apis/获取服务帐户密钥凭据(您还可以在此处限制允许此密钥执行的操作)

    1. 创建时,请务必点击提供新的私钥
    2. 在询问您如何下载密钥时选择 JSON.

  2. 您刚刚生成的服务帐户密钥包括一个client_email.

    1. 转到您的 google 电子表格并允许此 client_email 对该文档具有写入权限

  3. 使用以下代码进行身份验证

    <代码>让 jwtClient = new google.auth.JWT(client_email, null, private_key, ["https://www.googleapis.com/auth/spreadsheets",]);//验证请求jwtClient.authorize(function(err, tokens) {//此时身份验证完成,您现在可以使用 `jwtClient`//读取或写入电子表格});

client_emailprivate_keyservice 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.

  1. Get a service account key from https://console.cloud.google.com/apis/credentials (You can here also restrict what this keys is allowed todo)

    1. When creating, make sure you click the Furnish a new private key
    2. Select JSON when it asks you how to download the key.

  2. The service account key you have just generated includes a client_email.

    1. Go to you google spreadsheet and allow this client_email to have write access on this document

  3. 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屋!

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