【问题标题】:How can I fix an error on update Google spreadsheet如何修复更新 Google 电子表格时出现的错误
【发布时间】:2018-10-24 14:21:25
【问题描述】:

我在 PHP 框架上使用 Google Sheet Api 并在更新工作表时遇到错误。

Fatal error: Uncaught Google_Service_Exception: {
  "error": {
    "code": 403,
    "message": "Request had insufficient authentication scopes.",
    "errors": [
      {
        "message": "Request had insufficient authentication scopes.",
        "domain": "global",
        "reason": "forbidden"
      }
    ],
    "status": "PERMISSION_DENIED"
  }

我该如何解决?

function add($spreadsheetId, $range, $value, $service){
    $result = $service->spreadsheets_values->get($spreadsheetId, $range);
    $numRows = $result->getValues() != null ? count($result->getValues()) : 0;
    printf("%d rows retrieved.", $numRows);
    $body = new Google_Service_Sheets_ValueRange([
        'values' => [$value]
    ]);
    $end = chr(65 + count($value) - 1);
    $range = $range."!A".($numRows + 1).":".$end;//ex:Sheet!A6:H
    printf("%s\n", $range);
    $result = $service->spreadsheets_values->update($spreadsheetId, $range, $body, ['valueInputOption' => 'USER_ENTERED']);
    printf("%d cells added.\n", $result->getUpdatedCells());
}

【问题讨论】:

  • 请向我们展示您的代码和失败的请求。
  • 我已经创建了“添加”功能来通过调用它来更新电子表格,但正如我之前发布的那样,该功能不起作用并且出现错误代码。
  • 您是否通过 Google 验证了您的应用?

标签: php google-sheets-api


【解决方案1】:

我也有这样的错误。 我通过再次下载 credential.json 文件解决了这个问题。 此外,请检查电子表格 ID 是否正确或写入 $client->setScopes(Google_Service_Sheets::SPREADSHEETS);在创建服务之前。

【讨论】:

  • 太棒了!我再次下载了凭据文件并添加了您提供的代码,并且该功能运行良好。谢谢你的回答。
猜你喜欢
  • 1970-01-01
  • 2014-07-04
  • 2018-10-28
  • 1970-01-01
  • 2020-11-24
  • 1970-01-01
  • 1970-01-01
  • 2017-06-18
  • 1970-01-01
相关资源
最近更新 更多