【发布时间】:2016-10-24 09:21:27
【问题描述】:
我正在使用服务帐户身份验证来使用 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);
工作表正在创建,我收到响应
[spreadsheetId] => 34534534
但是,我无法通过浏览器使用我的 google 帐户打开此文件,即使我在 Google 的开发者控制台中添加为所有者、编辑者、作者和读者,在项目的权限中也是如此。浏览器说我未经授权,我可以联系管理员获取权限
有什么建议吗?有没有人管理过如何创建文档并为任何“人类”提供访问权限?
【问题讨论】:
-
您在创建文件时以谁身份进行身份验证? goole-api-services 是什么意思?另外请添加 $service 的创建,以便我们可以更轻松地查看您的身份验证方式。
-
@DalmTo,我正在使用在开发人员控制台中创建的服务帐户。它看起来像:project-name@positive-water-134xxx.iam.gserviceaccount.com
标签: php google-api google-sheets google-api-php-client service-accounts