我正在嘗試將我們的用戶個人資料從我們的內部 SaaS 同步到 Google Workspace 用戶個人資料。尤其是(性別、電話、職位、部門)。看了半天,發現在谷歌云專案中OAuth是不行的,但是需要創建一個服務賬號。我已經創建了它,但我仍然收到回應Not Authorized to access this resource/api。
- 啟用域范圍委派。
- 管理 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('[email protected]');
$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('[email protected]', $googleUser);
uj5u.com熱心網友回復:
使用具有域范圍委派的服務帳戶時,您需要模擬具有必要授權的用戶
- 目錄 API 方法users.update只能由具有相應角色/權限的域管理員執行。
- 了解如何使用戶成為管理員。
- 如有疑問,您可以使用授權為“[email protected]”的 [Try this API](Try this API) 進行測驗,以驗證該用戶是否具有必要的權限。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/421047.html
標籤:
