如何修复更新Google电子表格时出现的错误

时间:2018-10-24 14:21:26

标签: php google-sheets-api

我在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());
}

1 个答案:

答案 0 :(得分:0)

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