I am using a service account authentication to create a google sheet using the Google Sheets API. I want to create a spreadsheet and somehow allow my team to open it.
$private_key = file_get_contents($rootDir . '/config/googleApiCredentials.p12');
$client_email = 'project-names@positive-water-134xxx.iam.gserviceaccount.com';
$scopes = implode(' ', ["https://www.googleapis.com/auth/drive"]);
$credentials = new \Google_Auth_AssertionCredentials(
$client_email,
$scopes,
$private_key
);
// tried once with additional constructor params:
// $privateKeyPassword = 'notasecret',
// $assertionType = 'http://oauth.net/grant_type/jwt/1.0/bearer',
// $sub = 'myMail@gmail.com
$this->googleClient = new \Google_Client();
$this->googleClient->setAssertionCredentials($credentials);
if ($this->googleClient->getAuth()->isAccessTokenExpired()) {
$this->googleClient->getAuth()->refreshTokenWithAssertion();
}
$service = new \Google_Service_Sheets($this->googleClient);
$newSheet = new \Google_Service_Sheets_Spreadsheet();
$newSheet->setSpreadsheetId('34534534'); // <- hardcoded for test
$response = $service->spreadsheets->create($newSheet);
print_r($response);
The sheet is beeing created, I receive response with
[spreadsheetId] => 34534534
However, I can't open this file through browser, using my google's account, even when Im added as the owner, editor, writer and reader in Google's developer console, in project's permissions. Browser says that I'm unathorized and I can contact administrator for permissions
Any suggestions? Has anyone managed how to create documents and give access to it for any "human"?