问题
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'
回答1:
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!
来源:https://stackoverflow.com/questions/27057265/google-groups-directory-api-add-user-to-group-raises-error-php