Skip to content
Advertisement

Updating spreadsheet data with Google Spreadsheet api and php suddenly not working with authentication error

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(GuzzleHttpPsr7Response), Object(GuzzleHttpPsr7Request), 'Google_Service_...') 
#1 [internal function]: Google_Http_REST::doExecute(Object(GuzzleHttpClient), Object(GuzzleHttpPsr7Request), '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 GuzzleHttpClient([
            "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";
}

?>

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement