1

如何使用此功能?我有一个用户 ID 和一个组 ID。

当我尝试添加我的字段时收到的错误消息与 Google_Member 实例有关。我将如何在我的 PHP 代码中使用它?

顺便说一句,它来自 Google Apps API

/**
 * Add user to the specified group. (members.insert)
 *
 * @param string $groupKey Email or immutable Id of the group
 * @param Google_Member $postBody
 * @param array $optParams Optional parameters.
 * @return Google_Member
 */
public function insert($groupKey, Google_Member $postBody, $optParams = array()) {
  $params = array('groupKey' => $groupKey, 'postBody' => $postBody);
  $params = array_merge($params, $optParams);
  $data = $this->__call('insert', array($params));
  if ($this->useObjects()) {
    return new Google_Member($data);
  } else {
    return $data;
  }
}
4

2 回答 2

2

按照此处列出的说明在 Google Console 中设置应用程序并启用域授权。

http://jamespeckham.com/wordpress/?p=9(谢谢 JDPeckham)

从以下网址下载客户端:https ://code.google.com/p/google-api-php-client/downloads/list

这是我的工作代码:

require_once "google-api-php-client/src/Google_Client.php";
require_once "google-api-php-client/src/contrib/Google_DirectoryService.php";
require_once "google-api-php-client/src/contrib/Google_Oauth2Service.php";
session_start();

const GROUP_SCOPE = 'https://www.googleapis.com/auth/admin.directory.group';
const SERVICE_ACCOUNT_EMAIL = '.....@developer.gserviceaccount.com';
const SERVICE_ACCOUNT_PKCS12_FILE_PATH = '/path/to/...privatekey.p12';
const CLIENT_ID = '....apps.googleusercontent.com';

$userEmail = 'email-address-with-admin-rights@domain.com';

$key = file_get_contents(SERVICE_ACCOUNT_PKCS12_FILE_PATH);
$auth = new Google_AssertionCredentials(SERVICE_ACCOUNT_EMAIL, array(GROUP_SCOPE), $key, 'notasecret',  'http://oauth.net/grant_type/jwt/1.0/bearer', $userEmail);
$client = new Google_Client();
$client->setClientId(CLIENT_ID); // from API console
$client->setApplicationName("Project Name from API Console");
$client->setUseObjects(true);
$client->setAssertionCredentials($auth);

$member = new Google_Member(array('email' => 'abc@testing.com',
                        'kind' => 'admin#directory#member',
                        'role' => 'MEMBER',
                        'type' => 'USER'));
$service = new Google_DirectoryService($client);
$results = $service->members->insert('mailing-list-name@domain.com', $member);
print '<h2>Response Result:</h2><pre>' . print_r($results, true) . '</pre>';
于 2013-10-11T18:22:27.380 回答
0
require_once $_SERVER['DOCUMENT_ROOT'] . '/../vendor/autoload.php';


$updateName = $_POST["name"];
$updateEmail = $_POST["email"];

//USING A SERVICE ACCOUNT TO CALL GOOGLE CLIENT 
putenv('GOOGLE_APPLICATION_CREDENTIALS=/home/myserver/mywebsite/credentialsfile.json');

$client = new Google\Client();

 // MY ACCOUNT DATA HERE
$client_id = 'clientid';
$service_account_name = 'serviceaccountemailhere'; //Email Address 
$key_file_location = '/home/myserver/mywebsite/mykeyfile.json'; //key.p12
$agencyAdminGroupKey = 'emailforGroupKey@gmial.com'; //agency admins group key
$memberKey = $updateEmail; //$memberemail
$domain = 'yourwebsite.com';
if (getenv('GOOGLE_APPLICATION_CREDENTIALS')) {
  // use the application default credentials
  $client->useApplicationDefaultCredentials();
} else {
    echo missingServiceAccountDetailsWarning();
    return;
  }

// set the scope(s) that will be used
//$client->setScopes(array('https://www.googleapis.com/auth/admin.directory.group'));
$client->setScopes(array('https://www.googleapis.com/auth/admin.directory.group'));

// this is needed only if you need to perform
// domain-wide admin actions, and this must be
// an admin account on the domain; it is not 
// necessary in your example but provided for others
$client->setSubject('yourgoogleaccountemail@domain.com');

$client->setApplicationName("Client_Library_Examples");
$client->setDeveloperKey("paste-your-developer-key");

$service = new Google_Service_Directory($client);
$member = new Google_Service_Directory_Member( array('email' => $updateEmail,
                        'kind' => 'admin#directory#member',
                        'role' => 'MEMBER',
                        'type' => 'USER',
                        "deliverySettings" => 'DAILY'));
//GET request to google groups server
 // $results = $service->members->listMembers($groupKey);
  $results = $service->members->insert($agencyAdminGroupKey, $member);

以下是一些有用的链接:

于 2021-07-20T07:41:30.007 回答