gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 09:01:38 30 4
gpt4 key购买 nike

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

当用户加入我们的网站时,我们希望在新用户注入(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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com