php - How to restrict specific google user can read/write the google sheet?


I have a google sheet, I follow the step of PHP Quickstart ( to code the PHP script to read/write a Google sheet. I don't know how to manage the read/write permission.

Here is the code of the PHP script

$client = new Google_Client();
$client->setPrompt('select_account consent');
$spreadsheetId = '<my sheet id>';
$options = array('valueInputOption' => 'RAW');
$updateRange = "FormRange!F1:F3";
$requestBody = new Google_Service_Sheets_ValueRange();
$response = $service->spreadsheets_values->update($spreadsheetId, $updateRange, $requestBody, $options);

I have 3 Google accounts, e.g. GooA, GooB and GooC. There is a Google Sheet, User GooA is the owner of the sheet.

I deliberately don't share the sheet to anyone with the read/write access. I try to delete the token.json from the server and rebuild it with account GooB login and paste the verification code.

I get Error 500 when I execute the PHP script by all users including the owner GooA. I share the sheet to user GooC with can edit permission, Error 500 is still exist until I share the sheet to GooB with can edit permission. I read the log and find the Error 500 is about the permission issue.

Once I share the sheet to the user GooB who generates the verification code with can edit permission, all users including anonymous (without login with Google account) can execute the PHP script without error.

In the PHP script, it has the code to write/get values to/from the sheet, how can I grant the specific user can execute the script?

Best regards,





You can read from this documentation on how to Authorize Requests. When your application requests private data, the request must be authorized by an authenticated user who has access to that data.

There are two ways to identify your application:

  • using an OAuth 2.0 token (which also authorizes the request)
  • and/or using the application's API key. Here's how to determine which of those options to use:

Here's also another way to set the type of permission to users, check this SO post for more details.

People are also looking for solutions to the problem: php - Exception in RainLab Blog: Method (getRouter) Not Found


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.