【发布时间】:2022-01-25 21:00:41
【问题描述】:
我正在尝试将我们的用户个人资料从我们的内部 SaaS 同步到 Google Workspace 用户个人资料。尤其是(性别、电话、职位、部门)。 看了半天,发现在谷歌云项目中通过OAuth是不行的,但是需要创建一个服务账号。我已经创建了它,但我仍然收到响应无权访问此资源/api。
- 已启用域范围委派。
- Admin SDK API 已启用。
- 在 Workspace 安全性中启用了范围为“https://www.googleapis.com/auth/admin.directory.user”的 API 客户端访问。
代码:
$config = __DIR__ . '/project-users.json';
$client = new \Google\Client();
$client->setApplicationName('project-users');
$client->setAuthConfig($config);
$client->addScope(Google_Service_Directory::ADMIN_DIRECTORY_USER);
$client->setSubject('admin@domain.com');
$client->setAccessType('offline');
$gsdService = new \Google\Service\Directory($client);
$googleUser = new \Google\Service\Directory\User();
// Gender
$gender = new \Google\Service\Directory\UserGender();
$gender->setType('male');
// Phone
$phone = new \Google\Service\Directory\UserPhone();
$phone->setType('mobile');
$phone->setValue('123456789');
$googleUser->setPhones([$phone]);
// jobTitle and department
$organization = new \Google\Service\Directory\UserOrganization();
$organization->setPrimary(TRUE);
$organization->setTitle('Lead Developer');
$organization->setDepartment('Dev');
$googleUser->setOrganizations([$organization]);
$gsdService->users->update('fname.lname@domain.com', $googleUser);
【问题讨论】:
标签: php google-api google-api-php-client google-admin-sdk