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

问题描述 投票:2回答:1

哇,我搜索过高低,无法找到这个问题的“好”答案。这让我相信它非常容易,没有人在问,我是愚蠢的,或者没有人能弄明白。

当用户加入我们的网站时,我们希望在新的用户注入过程中也将它们添加到我们的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 = '[email protected]';

$newUser = array( "email" => "[email protected]", "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

* 解决了 *

令人厌恶的是,谷歌整个系统如何是一个大乱的混乱,没有答案,我设法搞清楚了。如果其他人有这个问题,就是这样。

Inserting a new Member into your Google Groups WITHOUT Oauth Login.

<?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 = "[email protected]"; // Your google groups "[email protected]"

$service = new Google_Service_Directory($client);
$user = new Google_Service_Directory_Member(array('email' => '[email protected]',
                    'kind' => 'admin#directory#member',
                    'role' => 'MEMBER',
                    'type' => 'USER'));


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

现在我还必须提一下,我遇到问题的主要原因是因为我没有正确设置域范围授权。你可以找到如何做到here

干杯。

php google-api google-api-php-client google-admin-sdk
1个回答
1
投票

* 解决了 *

令人厌恶的是,谷歌整个系统如何是一个大乱的混乱,没有答案,我设法搞清楚了。如果其他人有这个问题,就是这样。

Inserting a new Member into your Google Groups WITHOUT Oauth Login.

<?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 = "[email protected]"; // Your google groups "[email protected]"

$service = new Google_Service_Directory($client);
$user = new Google_Service_Directory_Member(array('email' => '[email protected]',
                    'kind' => 'admin#directory#member',
                    'role' => 'MEMBER',
                    'type' => 'USER'));


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

现在我还必须提一下,我遇到问题的主要原因是因为我没有正确设置域范围授权。你可以找到如何做到here

干杯。

© www.soinside.com 2019 - 2024. All rights reserved.