0
votes

I have a google sheet, I follow the step of PHP Quickstart (https://developers.google.com/sheets/api/quickstart/php) 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->setApplicationName('SurveyApp');
$client->setScopes(Google_Service_Sheets::SPREADSHEETS);
$client->setAuthConfig('credentials.json');
$client->setAccessType('offline');
$client->setPrompt('select_account consent');
$spreadsheetId = '<my sheet id>';
$options = array('valueInputOption' => 'RAW');
$updateRange = "FormRange!F1:F3";
$requestBody = new Google_Service_Sheets_ValueRange();
$requestBody->setValues([["Blue"],["Green"],["Red"]]);
$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,

Kelvin.

1

1 Answers

0
votes

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.