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

流过昼夜 提交于 2019-12-11 02:49:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!