1
votes

I want to write data to Google Sheet.

I can get data from my GoogleSheet successfully. But I try to write data I got the following error.

An uncaught Exception was encountered

Type: Google_Service_Exception

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

This is my code.

    $client = new \Google_Client();
    $client->setApplicationName('SheetsPHP');
    $client->setScopes([\Google_Service_Sheets::SPREADSHEETS]);

    $client->setAccessType('offline');

    $client->setAuthConfig(__DIR__ . '/../../credentials.json');
    $sheets = new \Google_Service_Sheets($client);

    $spreadsheetId = '.....';
    $range = '시트1!B2:G5';
    $values = [['b', 'c', 'd', 'e', 'f', 'g']];
    $body = new Google_Service_Sheets_ValueRange([
        'values' => $values
    ]);

    $params = [
        'valueInputOption' => "RAW"
    ];

    $result = $sheets->spreadsheets_values->update($spreadsheetId, $range, $body, $params);

I have shared the service account email already.

Please let me know what is wrong.

1

1 Answers

1
votes

just for testing try to modify your permission only for reading

$client->setScopes(Google_Service_Sheets::SPREADSHEETS_READONLY);

maybe the problem is with writing permissions