有没有办法进行批量请求以从多个或所有用户获取SendAs电子邮件?
目前,我们正在使用具有用户模拟的服务帐户来浏览每个用户并获取SendAs电子邮件列表 - 许多请求。
附:我在google网上发布了此帖,但只是看了一篇帖子说该论坛现在是只读的!奇怪的是它允许我发一个新帖子(显然我认为这个帖子必须得到批准)
谢谢!
static string[] Scopes = { GmailService.Scope.MailGoogleCom,
GmailService.Scope.GmailSettingsBasic,
GmailService.Scope.GmailSettingsSharing,
GmailService.Scope.GmailModify};
/// <summary>
/// Gets default send as email address from user's gmail - throws error if valid domain is not used as default sendAs
/// </summary>
/// <param name="primaryEmailAddress">User's email address to use to impersonate</param>
/// <param name="excludedDomains">Domains to exclude in the results - example: @xyz.org</param>
/// <returns>default SendAs email address</returns>
public static string GetDefaultSendAs(string primaryEmailAddress, string[] excludedDomains)
{
string retVal = string.Empty;
GmailService service = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer =
Auth.GetServiceAccountAuthorization
(scopes: Scopes, clientSecretFilePath: Constant.ClientSecretFilePath, impersonateAs: primaryEmailAddress)
});
var result = service.Users.Settings.SendAs.List("me").Execute();
SendAs s = result.SendAs.First(e => e.IsDefault == true);
bool incorrectSendAs = false;
if (s != null)
{
foreach (string domain in excludedDomains)
{
// Check if email ends with domain
if (s.SendAsEmail.ToLower().EndsWith("@" + domain.TrimStart('@'))) // removes @ and adds back - makes sure to domain start with @.
{
incorrectSendAs = true;
}
}
}
if (s != null && !incorrectSendAs)
retVal = s.SendAsEmail;
else
throw new Exception($"{primaryEmailAddress}, valid default SendAs email not set.");
System.Threading.Thread.Sleep(10);
return retVal;
}
授权码:
class Auth
{
internal static ServiceAccountCredential GetServiceAccountAuthorization(string[]scopes, string clientSecretFilePath, string impersonateAs = "[email protected]")
{
ServiceAccountCredential retval;
if (impersonateAs == null || impersonateAs == string.Empty)
{
throw new Exception("Please provide user to impersonate");
}
else
{
using (var stream = new FileStream(clientSecretFilePath, FileMode.Open, FileAccess.Read))
{
retval = GoogleCredential.FromStream(stream)
.CreateScoped(scopes)
.CreateWithUser(impersonateAs)
.UnderlyingCredential as ServiceAccountCredential;
}
}
return retval;
}
关于批处理的注意事项
首先,我要问你为什么要使用批处理。如果您希望它可以节省您的配额使用量,那么批量操作将受到与正常API调用相同的配额使用的影响。唯一的帮助批处理将使您发送更少的HTTP调用,并通过花费一些东西。
客户端所做的每个HTTP连接都会产生一定的开销。一些Google API支持批处理,允许您的客户端将多个API调用放入单个HTTP请求中。
外部批处理请求的HTTP标头(内容类型等内容标题除外)适用于批处理中的每个请求。如果在外部请求和单个调用中都指定了给定的HTTP标头,则单个调用标头的值将覆盖外部批处理请求标头的值。单个呼叫的标头仅适用于该呼叫。
例如,如果您为特定呼叫提供授权标头,则该标头仅适用于该呼叫。如果为外部请求提供Authorization标头,则该标头将应用于所有单个调用,除非它们使用自己的Authorization标头覆盖它。
授权
当您向api授权授权是针对单个用户时。
GmailService service = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer =
Auth.GetServiceAccountAuthorization
(scopes: Scopes, clientSecretFilePath: Constant.ClientSecretFilePath, impersonateAs: primaryEmailAddress)
});
上述服务只能访问您正在模拟的用户的一个用户数据。
回答
有没有办法进行批量请求以从多个或所有用户获取SendAs电子邮件?
不,那里没有。从上面可以看出,批处理请求的授权标题涵盖了批处理中的所有项目。与GmailService一起为批量请求发送的授权标题仅涵盖单个用户。