我正在使用服务帐户身份验证来使用Google表格API创建Google表格.我想创建一个电子表格,并以某种方式允许我的团队打开它.
$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);
Run Code Online (Sandbox Code Playgroud)
工作表正在创建,我收到回复
[spreadsheetId] => 34534534
Run Code Online (Sandbox Code Playgroud)
但是,即使我在Google的开发者控制台中添加了作为所有者,编辑,作者和读者的项目权限,我也无法通过浏览器使用我的谷歌帐户打开此文件.浏览器说我没有办法,我可以联系管理员获取权限
有什么建议?是否有人管理过如何创建文档并为任何"人类"提供访问权限?
php google-api google-sheets google-api-php-client service-accounts