首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用GMail API (PHP)为所有用户设置转发地址

使用GMail API (PHP)为所有用户设置转发地址
EN

Stack Overflow用户
提问于 2016-09-05 06:12:53
回答 2查看 1.3K关注 0票数 0

我正在为一个内部应用程序使用Gmail。我需要为所有使用管理帐户的用户设置电子邮件转发,我使用的是Yii中的Google客户端库。

我在关注这篇文章。https://developers.google.com/identity/protocols/OAuth2ServiceAccount

下面就是我想要做的。

代码语言:javascript
复制
define('JSON_FILE', '/path/to/json/file');
$user_to_impersonate = '<My domain wide authorized admin email address>';
$scopes = [ \Google_Service_Gmail::GMAIL_SETTINGS_SHARING,\Google_Service_Gmail::MAIL_GOOGLE_COM];
$google_client = new \Google_Client();
$google_client->setAuthConfig(CLIENT_SECRET_PATH);
$google_client->setScopes($scopes);
$google_client->setSubject($user_to_impersonate);
$google_client->setIncludeGrantedScopes(true);

// setup the forwarding address
$service = new \Google_Service_Gmail($google_client);
$f = new \Google_Service_Gmail_AutoForwarding();
$f->setEnabled(TRUE);
$f->setEmailAddress('<my forwarding email address>');
$f->setDisposition("leaveInInbox");

$service->users_settings->updateAutoForwarding('me',$f)

我得到以下错误,

代码语言:javascript
复制
Unrecognized forwarding address

我知道有些事情不对:)。谁能让我知道你的专家的反应来解决这个问题,并使这个工作。我觉得我正在尝试为我正在使用的电子邮件地址设置转发,但是对于所有的用户来说,不是。但我想为组织Gmail帐户中的所有电子邮件设置相同的电子邮件地址。

提前感谢!

EN

回答 2

Stack Overflow用户

发布于 2016-10-21 06:46:16

您得到了这个错误:failedPrecondition: Unrecognized forwarding address,因为您还没有创建一个经过验证的转发地址。您需要首先使用 API创建一个转发地址。然后,您的用户需要验证该电子邮件地址,然后你可以转发电子邮件到它。

这种方法的问题在于,您需要这个授权范围gmail.settings.sharing才能做到这一点,而这又需要在域范围内授权授权。看看怎么在这里设置它。如果没有设置此委托,则在尝试创建转发地址:forbidden: Access restricted to service accounts that have been delegated domain-wide authority时会出现此错误。

因此,您需要执行两个步骤:

  1. 设置域授权
  2. 使用前先创建转发地址(通过API)

希望这能有所帮助。

票数 3
EN

Stack Overflow用户

发布于 2016-09-07 02:59:05

这可能只是格式错误,但请删除"\“字符,因为它可能会导致您遇到的错误。

如果您想将电子邮件转发到和从管理帐户到一个用户列表(全部),您可以使用ForwardingAddresses API。

请注意,邮件只能转发到注册和验证的电子邮件地址。这可能是你得到错误的原因。在调用forwardingAddress之前创建一个updateAutoForwarding,并希望它能够正常工作。

编码愉快!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39324776

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档