Google网上论坛目录API - 将用户添加到群组引发错误 - PHP

时间:2014-11-21 08:45:22

标签: php google-api google-api-php-client google-admin-sdk google-directory-api

我一直在尝试将成员添加到我的Google应用群组中。我正在尝试使用以下代码,但它会引发错误。不知道出了什么问题。

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);

错误:

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 个答案:

答案 0 :(得分:3)

您应该添加'电子邮件'在$ member对象中,因为它是POST请求在指定组中添加新成员的必填字段。

$member = new Google_Service_Directory_Member(array('email' => 'name@domain.com',
                        'kind' => 'admin#directory#member',
                        'role' => 'MEMBER',
                        'type' => 'USER'));

您可以参考此documentation

希望有所帮助!