php - 通过 API 将新成员添加到我们的 Google 管理目录组

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

哇,我到处搜索,但找不到这个问题的“好”答案。这让我相信这很容易,没有人问,而且我很愚蠢,或者没有人能弄清楚。

当用户加入我们的网站时,我们希望在新用户注入(inject)过程中也将他们添加到我们的 Google 群组页面。我这里有这段代码,用于列出用户(尝试这样做只是为了让我开始)

$start = microtime(true);
require_once __DIR__ . '/google-api-php-client-2.2.1/vendor/autoload.php';
session_start();
date_default_timezone_set('America/Los_Angeles');
$REDIRECT_URI = 'http://localhost:8080';
$KEY_LOCATION = __DIR__ . '/client_secret.json';
$TOKEN_FILE   = "token.txt";
$SCOPES = array(
  Google_Service_Directory::ADMIN_DIRECTORY_GROUP_MEMBER
);

$client = new Google_Client();
$client->setApplicationName("Testing stuff");
$client->setAuthConfig($KEY_LOCATION);

// Incremental authorization
$client->setIncludeGrantedScopes(true);
$client->setAccessType('offline');
$client->setRedirectUri($REDIRECT_URI);
$client->setScopes($SCOPES);
$client->setAccessToken($_SESSION['accessToken']);

这就是我所得到的,请怜悯我。

*** 已更新 ***

我现在已经走到这一步了,感觉我已经更接近了。

<?php

require_once('google-api-php-client-2.2.1_PHP54/vendor/autoload.php');
$impersonateUser = 'XXX';
define('SCOPES', implode(' ', array( 
Google_Service_Directory::ADMIN_DIRECTORY_GROUP_MEMBER) ));
putenv('GOOGLE_APPLICATION_CREDENTIALS=' . 'c_json' );

$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setSubject($impersonateUser);
$client->setScopes(SCOPES);

$dir = new Google_Service_Directory_Member($client);

$gPrimaryEmail = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4e2c212c0e2c212c602d2123" rel="noreferrer noopener nofollow">[email protected]</a>';

$newUser = array( "email" => "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4e2c212c2c37777e7c7f7e0e2c212c602d2123" rel="noreferrer noopener nofollow">[email protected]</a>", "role" => "MEMBER");

$results = $dir->members->insert($newUser);

print_r($results);

PHP Fatal error:  Call to a member function insert() on a non-object in /var/www/newUserForGroup/addUser.php on line 19

*** 已解决 ***

我对谷歌的整个系统是一团乱七八糟、答案为零的感到非常厌恶,但我设法弄清楚了这一点。如果其他人也遇到此问题,就是这样。

无需 Oauth 登录即可将新成员插入您的 Google 网上论坛。

<?php

require_once('google-api-php-client-2.2.1_PHP54/vendor/autoload.php');
$impersonateUser = 'USER_EMAIL_WITH_ADMIN_ACCESS';

define('SCOPES', implode(' ', array( 
Google_Service_Directory::ADMIN_DIRECTORY_GROUP_MEMBER))); // scope needed for function

putenv('GOOGLE_APPLICATION_CREDENTIALS=' . 'json_c' ); // your json file

$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setSubject($impersonateUser);
$client->setScopes(SCOPES);

$key = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5f2b373a3e3b3b2d3a2c2c1f2b373a3e3b3b2d3a2c2c713c3032" rel="noreferrer noopener nofollow">[email protected]</a>"; // Your google groups "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="307855515c445844594043705855515c44585e5f471e535f5d" rel="noreferrer noopener nofollow">[email protected]</a>"

$service = new Google_Service_Directory($client);
$user = new Google_Service_Directory_Member(array('email' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bad3d9d1dbd8d5defad9c8dbd4df94d9d5d7" rel="noreferrer noopener nofollow">[email protected]</a>',
                    'kind' => 'admin#directory#member',
                    'role' => 'MEMBER',
                    'type' => 'USER'));


$list = $service->members->insert($key, $user);

现在我还必须提到,我遇到问题的主要原因是我没有正确设置域范围委派。您可以了解如何做到这一点here

最佳答案

* 已解决 *

我对谷歌的整个系统是一团乱七八糟、答案为零的感到非常厌恶,但我设法弄清楚了这一点。如果其他人也遇到此问题,就是这样。

无需 Oauth 登录即可将新成员插入您的 Google 网上论坛。

<?php

require_once('google-api-php-client-2.2.1_PHP54/vendor/autoload.php');
$impersonateUser = 'USER_EMAIL_WITH_ADMIN_ACCESS';

define('SCOPES', implode(' ', array( 
Google_Service_Directory::ADMIN_DIRECTORY_GROUP_MEMBER))); // scope needed for function

putenv('GOOGLE_APPLICATION_CREDENTIALS=' . 'json_c' ); // your json file

$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setSubject($impersonateUser);
$client->setScopes(SCOPES);

$key = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="285c404d494c4c5a4d5b5b685c404d494c4c5a4d5b5b064b4745" rel="noreferrer noopener nofollow">[email protected]</a>"; // Your google groups "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="afe7cacec3dbc7dbc6dfdcefc7cacec3dbc7c1c0d881ccc0c2" rel="noreferrer noopener nofollow">[email protected]</a>"

$service = new Google_Service_Directory($client);
$user = new Google_Service_Directory_Member(array('email' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a2cbc1c9c3c0cdc6e2c1d0c3ccc78cc1cdcf" rel="noreferrer noopener nofollow">[email protected]</a>',
                    'kind' => 'admin#directory#member',
                    'role' => 'MEMBER',
                    'type' => 'USER'));


$list = $service->members->insert($key, $user);

现在我还必须提到,我遇到问题的主要原因是我没有正确设置域范围委派。您可以了解如何做到这一点here

干杯。

关于php - 通过 API 将新成员添加到我们的 Google 管理目录组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49742889/

相关文章:

python - 如何在 Python 中更有效地审核 GCP 存储桶中的数千个对象

javascript - 带有javascript的谷歌api。获取生日和性别

java - 如何使用 gmail api 获取访问 token

php - 如何获取事件的创建者?

javascript - 无法读取未定义的属性 'length' 很少发生

symfony - 在 symfony 3 中添加 Google API 客户端库

javascript - 按下按钮时从 sql &php 获取行内容,即使调用 id

javascript - 在php中提交表单后未收到值

php 中的 javascript - 失败

php - 将特定字母替换为范围数字并使用 php 在数据库中搜索