获取经过身份验证的用户Gmail API-PHP SDK的用户角色/权限

时间:2019-03-27 22:35:10

标签: php gmail-api gsuite

我正在尝试使用此PHP代码来充当电子邮件的角色(它是超级管理员):

$email = 'an email with the same domain';
$credentials_file = '../service-account.json';
putenv('GOOGLE_APPLICATION_CREDENTIALS='.$credentials_file);

// Initialize Google Client
$client = new Google_Client();

$client->useApplicationDefaultCredentials();
// scopes to change signature
$client->setScopes(['https://www.googleapis.com/auth/gmail.settings.basic',
'https://www.googleapis.com/auth/gmail.settings.sharing',
'https://www.googleapis.com/auth/gmail.readonly', 'https://www.googleapis.com/auth/userinfo.profile']);

// Initialize Gmail
$gmail = new Google_Service_Gmail($client);
// set signature
$signature = new Google_Service_Gmail_SendAs();
// update signature

我正在使用最新的PHP SDK。该电子邮件始终与超级管理员电子邮件具有相同的域,但他/她并不总是超级管理员。

我尝试过的是:

var_dump($gmail->users->getProfile($email));

,但出现错误:

  

failedPrecondition,       消息:错误的请求”

我从这里读到Google Docs,我需要列出的权限之一,并且拥有https://www.googleapis.com/auth/gmail.readonly,我已经将其添加到Google管理员控制台中以供超级管理员用户使用。

如何获得具有相同域的用户电子邮件的角色?

1 个答案:

答案 0 :(得分:1)

基于the PHP API client docs,您需要在调用$client->setSubject($email)之前添加new Google_Service_Gmail($client),因为在调用Gmail API之前需要模拟用户。

相关问题