使用 Google 电子表格 api 和 php 更新电子表格数据突然无法使用身份验证错误
Updating spreadsheet data with Google Spreadsheet api and php suddenly not working with authentication error
在接受输入并更新电子表格中的值的表单中。之前它工作正常,但突然停止使用此错误消息:
Fatal error: Uncaught exception 'Google_Service_Exception' with message '{ "error": { "code": 403, "message": "The caller does not have permission", "errors": [ { "message": "The caller does not have permission", "domain": "global", "reason": "forbidden" } ], "status": "PERMISSION_DENIED" } } ' in /google-api-php-client-2.2.2/src/Google/Http/REST.php:118
Stack trace:
#0 /google-api-php-client-2.2.2/src/Google/Http/REST.php(94): Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...')
#1 [internal function]: Google_Http_REST::doExecute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...')
#2 /google-api-php-client-2.2.2/src/Google/Task/Runner.php(176): call_user_func_array(Array, Array)
#3 /google-api-php-client-2.2.2/src/Google/Http/REST.php(58): Google_Task_Runner->run()
#4 /html/form in /google-api-php-client-2.2.2/src/Google/Http/REST.php on line 118
根据其他问题和答案,这是因为身份验证问题,但该表格已经使用相同的身份验证工作了 5 年,因此令人困惑。是否还有其他原因导致表单未更新?
这是包含的代码
<?php
ini_set("display_errors", 1);
ini_set("display_startup_errors", 1);
error_reporting(E_ALL);
date_default_timezone_set("US/Central");
// Autoload Composer.
if (file_exists(__DIR__ . "/google-api-php-client-2.2.2/vendor/autoload.php")) {
require_once __DIR__ . "/google-api-php-client-2.2.2/vendor/autoload.php";
$spreadsheetId = "********"; // TODO: Update placeholder value.
// The A1 notation of a range to search for a logical table of data.
// Values will be appended after the last row of the table.
$range = "A2"; // TODO: Update placeholder value.
// TODO: Assign values to desired properties of `requestBody`:
$values = [
[
date("Y-m-d H:i:s"),
$_POST["prop_type"],
$_POST["pstreet"],
$_POST["pcity"],
$_POST["pzip"],
],
];
$service_account_file = "service-account.json";
$client = new Google_Client();
$service = new Google_Service_Sheets($client);
if ($client) {
$client->setApplicationName("Google Sheet Update");
$client->setAuthConfig($service_account_file);
$client->setScopes(Google_Service_Sheets::SPREADSHEETS);
$client->setAccessType("online");
$redirect_uri =
"http://" . $_SERVER["HTTP_HOST"] . $_SERVER["PHP_SELF"];
$client->setRedirectUri($redirect_uri);
$guzzle = new GuzzleHttp\Client([
"verify" => false,
]);
$client->setHttpClient($guzzle);
$requestBody = new Google_Service_Sheets_ValueRange([
"values" => $values,
]);
$params = [
"valueInputOption" => "RAW",
];
$response = $service->spreadsheets_values->append(
$spreadsheetId,
$range,
$requestBody,
$params
);
//echo '<pre>', var_export($response, true), '</pre>', "\n";
} else {
echo "Not Valid Client";
echo "<pre>CLIENT", var_dump($client), "</pre>", "\n";
}
} else {
echo "Client File do not exist";
}
?>
The caller does not have permission
就是这个意思。您用来授权此代码的用户无权访问该 sheet。向具有访问权限的用户授权您的应用程序或授予该用户访问权限。
服务帐号需要预授权。最常见的方法是获取服务帐户客户端 ID 并通过 google 驱动器 Web 应用程序与其共享文件。如果有人删除了服务帐户对该文件的访问权限。服务帐户将不再具有访问权限。
我会仔细检查它是否仍然可以访问。
在接受输入并更新电子表格中的值的表单中。之前它工作正常,但突然停止使用此错误消息:
Fatal error: Uncaught exception 'Google_Service_Exception' with message '{ "error": { "code": 403, "message": "The caller does not have permission", "errors": [ { "message": "The caller does not have permission", "domain": "global", "reason": "forbidden" } ], "status": "PERMISSION_DENIED" } } ' in /google-api-php-client-2.2.2/src/Google/Http/REST.php:118
Stack trace:
#0 /google-api-php-client-2.2.2/src/Google/Http/REST.php(94): Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...')
#1 [internal function]: Google_Http_REST::doExecute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...')
#2 /google-api-php-client-2.2.2/src/Google/Task/Runner.php(176): call_user_func_array(Array, Array)
#3 /google-api-php-client-2.2.2/src/Google/Http/REST.php(58): Google_Task_Runner->run()
#4 /html/form in /google-api-php-client-2.2.2/src/Google/Http/REST.php on line 118
根据其他问题和答案,这是因为身份验证问题,但该表格已经使用相同的身份验证工作了 5 年,因此令人困惑。是否还有其他原因导致表单未更新?
这是包含的代码
<?php
ini_set("display_errors", 1);
ini_set("display_startup_errors", 1);
error_reporting(E_ALL);
date_default_timezone_set("US/Central");
// Autoload Composer.
if (file_exists(__DIR__ . "/google-api-php-client-2.2.2/vendor/autoload.php")) {
require_once __DIR__ . "/google-api-php-client-2.2.2/vendor/autoload.php";
$spreadsheetId = "********"; // TODO: Update placeholder value.
// The A1 notation of a range to search for a logical table of data.
// Values will be appended after the last row of the table.
$range = "A2"; // TODO: Update placeholder value.
// TODO: Assign values to desired properties of `requestBody`:
$values = [
[
date("Y-m-d H:i:s"),
$_POST["prop_type"],
$_POST["pstreet"],
$_POST["pcity"],
$_POST["pzip"],
],
];
$service_account_file = "service-account.json";
$client = new Google_Client();
$service = new Google_Service_Sheets($client);
if ($client) {
$client->setApplicationName("Google Sheet Update");
$client->setAuthConfig($service_account_file);
$client->setScopes(Google_Service_Sheets::SPREADSHEETS);
$client->setAccessType("online");
$redirect_uri =
"http://" . $_SERVER["HTTP_HOST"] . $_SERVER["PHP_SELF"];
$client->setRedirectUri($redirect_uri);
$guzzle = new GuzzleHttp\Client([
"verify" => false,
]);
$client->setHttpClient($guzzle);
$requestBody = new Google_Service_Sheets_ValueRange([
"values" => $values,
]);
$params = [
"valueInputOption" => "RAW",
];
$response = $service->spreadsheets_values->append(
$spreadsheetId,
$range,
$requestBody,
$params
);
//echo '<pre>', var_export($response, true), '</pre>', "\n";
} else {
echo "Not Valid Client";
echo "<pre>CLIENT", var_dump($client), "</pre>", "\n";
}
} else {
echo "Client File do not exist";
}
?>
The caller does not have permission
就是这个意思。您用来授权此代码的用户无权访问该 sheet。向具有访问权限的用户授权您的应用程序或授予该用户访问权限。
服务帐号需要预授权。最常见的方法是获取服务帐户客户端 ID 并通过 google 驱动器 Web 应用程序与其共享文件。如果有人删除了服务帐户对该文件的访问权限。服务帐户将不再具有访问权限。
我会仔细检查它是否仍然可以访问。