Google网上论坛目录API - 将用户添加到群组 - PHP功能

时间:2013-09-11 05:47:35

标签: php

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

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

BTW来自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;
  }
}

2 个答案:

答案 0 :(得分:1)

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

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>';

答案 1 :(得分:0)

我知道这是几年后的事,但对于仍在寻找答案的人来说,这对我有用。以下是一些有用的链接,它们也帮助我解决了这个问题: https://github.com/googleapis/google-api-php-client https://developers.google.com/admin-sdk/directory/v1/quickstart/php https://developers.google.com/resources/api-libraries/documentation/admin/directory_v1/php/latest/class-Google_Service_Directory_Groups.html https://developers.google.com/admin-sdk/directory/reference/rest/v1/members

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);

希望这对某人有帮助,如果有帮助,请点赞! -韦斯