【问题标题】:Google Groups Directory API - Add user to group raises error - PHPGoogle Groups Directory API - 将用户添加到组会引发错误 - PHP
【发布时间】:2014-11-21 08:45:21
【问题描述】:

我一直在尝试将成员添加到我的 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'

【问题讨论】:

  • Missing required field: memberKey
  • 放在哪里以及如何放置?
  • 我使用的是相同的代码。但它给了我这个错误 - 调用未定义的方法 Google_Client::setAssertionCredentials()

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


【解决方案1】:

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

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

你可以参考这个documentation。

希望有帮助!

【讨论】:

  • 非常感谢。尝试了整整一个小时试图找出缺少的东西。现在想知道我是怎么错过的。 :)
  • HI MECHANICWEB 和 KRR。我正在使用相同的代码。但它给了我这个错误 - 调用未定义的方法 Google_Client::setAssertionCredentials()
  • 如何查看群组中是否已经存在会员邮箱?
猜你喜欢
  • 1970-01-01
  • 2015-05-12
  • 2015-11-28
  • 2015-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多