【问题标题】:sheet API (google apis) returns error "'Invalid grant: account not found'工作表 API (google apis) 返回错误“'无效授权:找不到帐户'
【发布时间】:2020-04-22 09:12:39
【问题描述】:

我想通过 sheet api 从电子表格中获取数据。

我在 GCP 和服务帐户中创建项目。然后我授予该帐户编写电子表格的权限。 之后,我将凭据下载为 json。

我的node应用在这里

const sheets = google.sheets('v4');

  execAPI('[sheet_ID]', 'articles!C5:F8');

  async function execAPI(spreadsheetId: string, range: string) {
    const key = process.env.private_key === undefined ? undefined : process.env.private_key.replace(/\\n/g, "\n");
    const auth = await google.auth.getClient({
      credentials: {
        client_email: process.env.client_email,
        private_key: key,
      },
      scopes: 'https://www.googleapis.com/auth/spreadsheets.readonly',
    });

    const apiOptions = {
      auth,
      spreadsheetId,
      range,
    };

    sheets.spreadsheets.values.get(apiOptions, (err: Error | null, res: GaxiosResponse | undefined | null) => {
      console.log(err);
      console.log(res);
    });

我尝试了很多次,但是,只得到错误“无效授权:找不到帐户”

我该怎么办??请教我。

【问题讨论】:

    标签: typescript google-sheets google-api next.js google-sheets-api


    【解决方案1】:

    此错误表示授权授予或刷新令牌无效

    发生这种情况有几个原因 - 请参阅here,最有可能是您更改了范围:这样做后,您需要删除旧令牌文件,以便应用程序创建一个新的、更新的令牌。

    【讨论】:

    • 非常感谢!我终于通过重新创建服务帐户的凭据密钥并设置它们来连接 API!
    猜你喜欢
    • 2014-07-19
    • 2015-01-19
    • 1970-01-01
    • 1970-01-01
    • 2017-06-13
    • 1970-01-01
    • 2017-03-15
    • 1970-01-01
    • 2013-08-21
    相关资源
    最近更新 更多