Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Groups Directory API - Add user to group raises error - PHP

I have been trying to add members to my google apps group. I am trying with following code but it raises error. Don't know what is doing wrong.

include_once 'api-client/autoload.php';

$clientId = 'xxxxxxxxxxxxxxxxx.apps.googleusercontent.com';

$serviceAccountName = '[email protected]';

$delegatedAdmin = '[email protected]';

$keyFile = 'mw-gxxxxxxxx.p12';

$appName = 'Example App';

$scopes = array(
    'https://www.googleapis.com/auth/admin.directory.group'
);

$creds = new Google_Auth_AssertionCredentials(
    $serviceAccountName,
    $scopes,
    file_get_contents($keyFile)
);

$creds->sub = $delegatedAdmin;

$client = new Google_Client();
$client->setApplicationName($appName);
$client->setClientId($clientId);
$client->setAssertionCredentials($creds);

$dir = new Google_Service_Directory($client);


 $member = new Google_Service_Directory_Member(array('[email protected]',
                        'kind' => 'admin#directory#member',
                        'role' => 'MEMBER',
                        'type' => 'USER'));

$list = $dir->members->insert('01tuee7433xxxxx', $member);

The error:

Fatal error: Uncaught exception 'Google_Service_Exception' with message 
'Error calling POST https://www.googleapis.com/admin/directory/v1/groups/01tuee7433v8xwz/members: (400) Missing required field: memberKey'
like image 756
MECHANICWEB Avatar asked Aug 27 '26 13:08

MECHANICWEB


1 Answers

You should add 'email' in the $member object, as it is a required field for the POST request to add a new member in the specified group.

$member = new Google_Service_Directory_Member(array('email' => '[email protected]',
                        'kind' => 'admin#directory#member',
                        'role' => 'MEMBER',
                        'type' => 'USER'));

You can refer to this documentation.

Hope that helps!

like image 128
KRR Avatar answered Aug 30 '26 04:08

KRR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!