首页 > 解决方案 > Gmail API sendAsEmail 不是有效的用户或组

问题描述

我正在尝试使用 Gmail API 和 Google 管理员创建自定义“发件人”发送别名 (sendAsEmail):https ://developers.google.com/gmail/api/reference/rest/v1/users.settings.sendAs /创造

我收到 400 错误sendAsEmail is not a valid user or group

在 Google Admin 中,我有 3 个经过验证的域,所有域都使用 MX 正确注册了 gmail 服务:

@domain-a.com is primary domain
@domain-b.com (not a domain alias) 
@domain-c.com (not a domain alias)

user@domain-a.com是一个用户,没有定义 send-as 别名。当我创建 sendAsEmail 时user@domain-b.com,没有问题。我可以看到它出现在 gmail 中。当我创建 sendAsEmailuser@domain-c.com时,它失败了。我不知道为什么。如果我user@domain-c.com在 Google Admin 中搜索以确保它不是一个组,则没有结果。任何帮助都会非常受欢迎。

我正在使用 PHP 进行 api 调用,但我认为问题与语言无关。

标签: gmailgmail-apigoogle-admin-sdk

解决方案


终于找到了解决办法。实际上有多种原因:

谷歌管理员:

首先在 Google Admin 方面,我必须创建一个别名:https ://developers.google.com/admin-sdk/directory/v1/guides/manage-user-aliases 。如果您不这样做,Gmail sendAsEmail 创建将失败。请注意,用于执行此调用的客户端 HTTP 需要使用管理员用户:

<?php
putenv('GOOGLE_APPLICATION_CREDENTIALS=./client_secrets.json');
$this->client = new Google_Client();
$this->client->useApplicationDefaultCredentials();  // loads whats in that json service account file.
$this->client->setScopes([
  Google_Service_Directory::ADMIN_DIRECTORY_USER,
  Google_Service_Directory::ADMIN_DIRECTORY_USER_SECURITY,
  Google_Service_Directory::ADMIN_DIRECTORY_USER_ALIAS
]);
$this->client->setSubject('admin@domain.com');

邮箱:

创建一个 sendAsEmail,如下所述:https://developers.google.com/gmail/api/reference/rest/v1/users.settings.sendAs/create 请注意,用于执行此调用的客户端 Http 需要模拟用户:

<?php
putenv('GOOGLE_APPLICATION_CREDENTIALS=./client_secrets.json');
$this->client = new Google_Client();
$this->client->useApplicationDefaultCredentials();  // loads whats in that json service account file.
$this->client->setScopes([
  Google_Service_Gmail::GMAIL_SETTINGS_BASIC,
  Google_Service_Gmail::GMAIL_SETTINGS_SHARING
]);
$this->client->setSubject('user@domain.com');

推荐阅读