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



我正在尝试使用以下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));

,但是我收到错误:

失败前提条件, 消息:错误请求">

我从这里读到 - 谷歌文档,我需要列出的权限之一,我有 https://www.googleapis.com/auth/gmail.readonly ,我已将其添加到超级管理员用户的 Google 管理控制台中。

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

根据 PHP API 客户端文档,您需要在调用 new Google_Service_Gmail($client) 之前添加$client->setSubject($email),因为在调用 Gmail API 之前需要模拟用户。

最新更新