javascript - Google 表格,具有服务帐户的 JWT 客户端

标签 javascript node.js google-api jwt google-sheets-api

我正在拔头发!帮助 !! 更新:我使用的是 google-auth-library v1.0.0 和 googleapis v24.0.0。

const { JWT } = require('google-auth-library');
var google = require('googleapis');
var sheets = google.sheets('v4');

const client = new JWT({
  email: keys.client_email
  ,key: keys.private_key
  ,scopes: ['https://spreadsheets.google.com/feeds']
});

return new Promise(function(resolve, reject) {

  client.authorize()
  .then((obj) => {

  // fails at this call
  sheets.spreadsheets.values.append({

    auth: client
    ,range: "A1"
    ,spreadsheetId: SHEET_ID
    ,insertDataOptions: "INSERT_ROWS"
    ,responseDateTimeRenderOption: "FORMATTED_STRING"
    ,responseValueRenderOption: "UNFORMATTED_VALUE"
    ,valueInputOption: "RAW"
    ,resource: {
      values: [
        [1,2,3]
      ]
    }

.......为清楚起见省略了代码

我不断得到:

'json' 不是有效的配置选项。请改用“数据”。该库使用 Axios 进行请求。请参阅 https://github.com/axios/axios了解有关有效请求的更多信息 选项。在 Object.validate (/user_code/node_modules/google-auth- library/build/src/options.js:32:19) 在 JWT 的 DefaultTransporter.request (/user_code/node_modules/google-auth-library/build/src/transporters.js:49:23)。 (/user_code/node_modules/google-auth-library/build/src/auth/oauth2client.js:427:63) 在步骤 (/user_code/node_modules/google-auth-library/build/src/auth/oauth2client.js: 57:23) 在 Object.next (/user_code/node_modules/google-auth-library/build/src/auth/oauth2client.js:38:53) 在 fulfilled (/user_code/node_modules/google-auth-library/build/src/auth/oauth2client.js:29:58) 在 process._tickDomainCallback (internal/process/next_tick.js:135:7)

最佳答案

我按照 https://www.npmjs.com/package/googleapis 解决了它并且根本不使用 google-auth-library

var google = require('googleapis');
const client = new google.auth.JWT(
    keys.client_email,
    null,
    keys.private_key,
    ['https://www.googleapis.com/auth/spreadsheets'],
    null
);
return new Promise((resolve, reject) => {
    client.authorize((err, tokens) => {
        if (err) {
            reject(err);
        } else {
            google.options({
                auth: client
            });
            resolve();
        }
    });
});

//and then use client as you did

关于javascript - Google 表格,具有服务帐户的 JWT 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48242812/

相关文章:

javascript - Google Sheet 脚本重置下拉框

javascript - Dynamodb getItem 与 Node js 抛出错误

java - 编排多个 google API 客户端的最佳方法是什么?

google-api - 使用 Google API 时出现身份验证范围不足错误

javascript - 在街景 View 中设置标记并清除它们

javascript - 如何拦截 puppeteer 上的下载请求并读取被拦截的文件

javascript - 按字符分割数组

javascript - 使用 mysql 和 bcryptjs 时出现意外标识符语法错误

javascript -/node_modules/fibres/bin/linux-x64-v8-7.6/fibres.node` 丢失。尝试重新安装 `node-fibers`?

ruby - 如何使用服务帐户从 google drive api 下载文件?