【问题标题】:POST request fails with insufficient authentication scope, while GET request succeedsPOST 请求因身份验证范围不足而失败,而 GET 请求成功
【发布时间】:2023-03-14 16:15:01
【问题描述】:

我正在尝试使用 Google 表格 API 发出 POST 请求并更新我的表格。当我执行 GET 请求时,我的代码正在运行,但 POST 请求失败并出现以下错误:

API 返回错误:
错误:请求的身份验证范围不足

这是我的 POST 请求代码:

app.post('/', (req, res) => {
  fs.readFile('credentials.json', (err, content) => {
    if (err) return console.log('Error loading client secret file:', err)
    authorize(JSON.parse(content), postTest)
  })
  function postTest(auth) {
    const sheets = google.sheets({version: 'v4', auth})
    sheets.spreadsheets.values.update({
      spreadsheetId: 'spreadsheetId',
      range: 'A5',
      valueInputOption: 'Test',
    }, (err, res) => {
      if (err) return console.log('The API returned an error: ' + err)
      else {
        console.log('%d cells updated.', result.updatedCells);
      }
    })
  }
})

我的范围:

const SCOPES = ['https://www.googleapis.com/auth/spreadsheets']

【问题讨论】:

  • 能否提供函数authorize()的脚本?
  • 你有没有改变过这个项目的范围?比如从spreadsheets.readonly到当前作用域?这样做之后,您是否从相关目录中删除了任何序列化的凭证文件?请注意,所有更新请求 - 即使是公开的、匿名可写的电子表格 - 都需要使用 OAuth 访问令牌进行授权,而只需使用 API 密钥就可以充分授权读取。

标签: node.js authentication google-sheets-api google-api-nodejs-client


【解决方案1】:

问题来自 token.json 文件。更改范围后我忘记将其删除。问题解决了!

【讨论】:

    【解决方案2】:

    这几天我一直遇到同样的问题,我终于在我的文件系统中找到了它。我发誓,我四处张望,没有人给出我必须删除的文件的清晰路径。

    因此,对于无论出于何种原因找到使用 Sheets API 的新手程序员,我在以下位置找到了它:

    ~/Users/< MyUser >/source/repos/< MyVisualStudioProjectName >/< MyVisualStudioProjectName >/bin/Debug/token.json
    

    我删除了该文件夹中的Google.Apis.Auth.OAuth2.Responses.TokenResponse-user,我的写入请求开始工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-09
      • 2016-05-17
      • 2021-08-07
      • 2021-01-04
      • 2021-10-20
      • 2021-10-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多