Код: Выделить всё
{ "error": { "code": 403, "message": "Not Authorized to access this resource/api", "errors": [ { "message": "Not Authorized to access this resource/api", "domain": "global", "reason": "forbidden" } ] } }
- Создал сервисную учетную запись. Он отлично работает с определенными областями и другими конечными точками, такими как:
[*]Предоставьте владельцу и администратору безопасный доступ к субъекту сервисного аккаунта:

[*]Добавьте те же области моего кода в параметр «Делегирование на уровне домена» в консоли администратора Google Workspace

[*]Это мой код:
Код: Выделить всё
public function getClient()
{
$KEY_FILE_LOCATION = FCPATH . '/assets/api/useraccounts/user-accounts.json';
$client = new Google_Client();
$client->setAuthConfig($KEY_FILE_LOCATION);
$client->useApplicationDefaultCredentials();
$client->setSubject('email@company.com');
$client->setApplicationName("Google User Accounts");
$client->setScopes([
"https://www.googleapis.com/auth/admin.directory.group.readonly",
"https://www.googleapis.com/auth/admin.directory.group.member.readonly",
'https://www.googleapis.com/auth/admin.directory.user',
'https://www.googleapis.com/auth/admin.directory.user.readonly',
'https://www.googleapis.com/auth/admin.directory.user.security'
]);
return $client;
}
public function create()
{
$client = $this->google_account->getClient();
$service = new Google_Service_Directory($client);
$user = new Google_Service_Directory_User();
$name = new Google_Service_Directory_UserName();
$name->setGivenName('Apple');
$name->setFamilyName('Test');
$user->setName($name);
$user->setHashFunction("MD5");
$user->setPrimaryEmail("apply.test@company.com");
$user->setPassword(hash("md5","Apple2024"));
try
{
$createUserResult = $service->users->insert($user);
var_dump($createUserResult);
}
catch (Exception $e) {
echo $e->getMessage();
}
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... source-api