使用 Google 的 Node.js 工作表 API 时身份验证出错:提供的密钥文件未定义有效的重定向 URI

Error with authentication while using Google's Node.js Sheets API: The provided keyfile does not define a valid redirect URI

我正在尝试使用 Node.js Google 工作表 API 写入 google sheet,但是当我 运行脚本,使用下面引用的命令,我收到以下错误:

node append.js Spreadsheet-ID 0

Error: The provided keyfile does not define a valid
redirect URI. There must be at least one redirect URI defined, and this sample
assumes it redirects to 'http://localhost:3000/oauth2callback'.  Please edit
your keyfile, and add a 'redirect_uris' section.  For example:

"redirect_uris": [
  "http://localhost:3000/oauth2callback"
]

    at authenticate (/folder/node_modules/@google-cloud/local-auth/build/src/index.js:56:15)
    at runSample (/folder/append.js:24:22)
    at Object.<anonymous> (/folder/append.js:52:3)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)

我在他们的 googleapis Github repository.

中使用 Google 提供的 append.js 脚本

我的 oauth2.keys.json 文件:

{
    "web": {
        "client_id": "my-client-id.apps.googleusercontent.com",
        "project_id": "my-id",
        "auth_uri": "https://accounts.google.com/o/oauth2/auth",
        "token_uri": "https://oauth2.googleapis.com/token",
        "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
        "client_secret": "client_secret",
        "redirect_uris": [
            "https://my-site.net"
        ]
    }
}

您需要一个有效重定向URI

我知道,这就是错误所说的,很抱歉我只是重复一遍,但是,这是您被绊倒的线索。

应用授权成功后重定向URI不能只是"https://my-site.net"。 URI 中的资源需要能够处理 OAuth2 响应,该响应将包含授权代码或错误响应。

Here 是 Google 处理服务器响应指南的 link。

Here 是 Github Repo 的一个问题,linked 因为它是一个很好的案例研究,可以帮助你解决与你的问题相似的问题你弄清楚了如何验证你的应用程序。

Google 提供的 Oauth2 docs 提供了广泛的信息和指南以提供帮助。