【问题标题】:Google Sheets, JWT client with Service AccountGoogle 表格,具有服务帐户的 JWT 客户端
【发布时间】:2018-06-22 21:25:49
【问题描述】:

我要拔头发了!帮助 !! 更新:我正在使用 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) 在完成 (/user_code/node_modules/google-auth-library/build/ src/auth/oauth2client.js:29:58) 在 process._tickDomainCallback (internal/process/next_tick.js:135:7)

【问题讨论】:

  • 感谢您在没有解释的情况下对我的合法帖子投反对票。非常好的手势。
  • 自从升级到最新版本后,我遇到了同样的问题。您是否更接近解决方案?

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


【解决方案1】:

我通过关注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

【讨论】:

  • 感谢 Stefan 花时间写这篇文章。我遇到的问题不在于身份验证部分,而在于通过 googleapis 库访问 google sheet API。如果我正确理解了 Github 上的通信,则该库的 v25.0.0 应该与 google-auth-library v1.0.0 保持一致。交叉我的手指。
  • 我的问题还在于访问工作表。我遇到了与您最初发布的完全相同的问题。这为我修好了。你试过了吗?
  • 您使用的是哪个版本的库?
  • 24.0.0 用于 googleapis。我不再使用 google-auth-library。
  • 你是我的英雄!!谢谢斯特凡!
猜你喜欢
  • 1970-01-01
  • 2021-02-16
  • 1970-01-01
  • 2019-02-28
  • 1970-01-01
  • 2020-06-09
  • 2021-04-26
  • 2015-02-26
  • 1970-01-01
相关资源
最近更新 更多