使用 Google Spreadsheet api 和 php 更新电子表格数据突然无法正常工作并出现身份验证错误

发布于 2025-01-12 02:16:18 字数 3307 浏览 4 评论 0原文

采用接受输入并更新电子表格中的值的形式。之前工作正常,但突然停止工作并出现此错误消息:

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";
}

?>

In a form which takes input and updates the value in a spreadsheet. It was working fine before but suddenly stopped working with this error message:

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

According to other questions and answers it is because of authentication problem, but the form was working for 5 years with the same authentication so it is confusing. Is there any other reason for which the form is not updating?

here is the code included

<?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";
}

?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生寂 2025-01-19 02:16:18

调用者没有权限

正是这个意思。您用于授权此代码的任何用户都无权访问该工作表。向具有访问权限的用户授权您的应用程序或授予该用户访问权限。

服务帐户需要预先授权。最常见的方法是获取服务帐户客户端 ID 并通过 Google Drive Web 应用程序与其共享文件。如果有人删除了对该文件的服务帐户访问权限。服务帐户将不再具有访问权限。

我会仔细检查它是否仍然可以访问。

The caller does not have permission

means exactly that. Which ever user you used to authorize this code does not have permission to access that sheet. Authorize your application with a user that has access or grant that user access.

Service accounts need to be preauthorized. The most common way to do that is to take the service account client id and share the file with it though the google drive web application. If someone removed the service accounts access to the file. The service account will no longer have access.

I would double check that it still has access.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文