通过API

时间:2018-04-09 22:57:01

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

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

当用户加入我们的网站时,我们希望在新用户注入过程中也将其添加到我们的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 = 'bob@bob.com';

$newUser = array( "email" => "bobby90210@bob.com", "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 = "theaddress@theaddress.com"; // Your google groups "Healthtips@healthnow.com"

$service = new Google_Service_Directory($client);
$user = new Google_Service_Directory_Member(array('email' => 'ickabod@crane.com',
                    'kind' => 'admin#directory#member',
                    'role' => 'MEMBER',
                    'type' => 'USER'));


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

现在我还必须提一下,我遇到问题的主要原因是因为我没有正确设置域范围授权。您可以了解如何执行此操作here

干杯。

1 个答案:

答案 0 :(得分:1)

*已解决*

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

在没有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 = "theaddress@theaddress.com"; // Your google groups "Healthtips@healthnow.com"

$service = new Google_Service_Directory($client);
$user = new Google_Service_Directory_Member(array('email' => 'ickabod@crane.com',
                    'kind' => 'admin#directory#member',
                    'role' => 'MEMBER',
                    'type' => 'USER'));


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

现在我还必须提一下,我遇到问题的主要原因是因为我没有正确设置域范围授权。您可以了解如何执行此操作here

干杯。