gpt4 book ai didi

c# - 批量请求 - SendAs 电子邮件

转载 作者:太空宇宙 更新时间:2023-11-03 14:44:55 33 4
gpt4 key购买 nike

有没有办法批量请求从多个或所有用户获取 SendAs 电子邮件?

目前,我们正在使用具有用户模拟功能的服务帐户来遍历每个用户并获取 SendAs 电子邮件列表 - 大量请求。

  1. 作为服务的 GmailService - 这被模拟为用户。
  2. service.Users.Settings.SendAs.List("me").Execute();

附言我在 google group 中发布了这个,但刚刚看到一篇帖子说论坛现在是只读的!奇怪的是它允许我发布新帖子(显然我认为该帖子必须获得批准)

谢谢!

    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 = "admin@xyz.org")
{
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 客户端访问: API client access

最佳答案

批处理注意事项

首先我要问你为什么要使用批处理。如果您希望它能节省您的配额使用量,那么批处理不会受到与普通 api 调用相同的配额使用量的影响。批处理给你的唯一帮助是发送更少的 HTTP 调用,并在那里花费一点点。

客户端建立的每个 HTTP 连接都会产生一定的开销。一些 Google API 支持批处理,以允许您的客户端将多个 API 调用放入单个 HTTP 请求中。

外部批处理请求的 HTTP header (Content- header (如 Content-Type)除外)适用于批处理中的每个请求。如果您在外部请求和单个调用中指定给定的 HTTP header ,则单个调用 header 的值将覆盖外部批处理请求 header 的值。单个调用的 header 仅适用于该调用。

例如,如果您为特定调用提供授权 header ,则该 header 仅适用于该调用。如果您为外部请求提供 Authorization header ,则该 header 适用于所有单个调用,除非它们使用自己的 Authorization header 覆盖它。

授权

当您向 api 授权时,该授权是针对单个用户的。

GmailService service = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer =
Auth.GetServiceAccountAuthorization
(scopes: Scopes, clientSecretFilePath: Constant.ClientSecretFilePath, impersonateAs: primaryEmailAddress)
});

上述服务将只能访问您正在模拟的用户的那个用户数据。

回答者

Is there a way to do a batch request to get SendAs emails from multiple or all users?

不,没有。从上面可以看出,批处理请求的授权 header 涵盖了批处理中的所有项目。与 GmailService 一起为您的批处理请求发送的授权 header 将仅涵盖单个用户。

关于c# - 批量请求 - SendAs 电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54868344/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com