Google Groups Directory API - Add user to group raises error - PHP
google-admin-sdk, google-api, google-api-php-client, google-directory-api, php
Solution
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' => 'name@domain.com',
'kind' => 'admin#directory#member',
'role' => 'MEMBER',
'type' => 'USER'));
You can refer to this documentation.
Hope that helps!
Problem
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 = 'xxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com'; $delegatedAdmin = 'superadmin@domain.com'; $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('name@domain.com', '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' ```