首页 > 解决方案 > 工作表 API (google apis) 返回错误“'无效授权:找不到帐户'

问题描述

我想通过 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);
    });

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

我应该怎么办??请教我。

标签: typescriptgoogle-sheetsgoogle-apinext.jsgoogle-sheets-api

解决方案


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

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


推荐阅读