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

3.3k views Asked by At

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'
1

There are 1 answers

3
KRR On BEST ANSWER

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!