批处理请求-SendAs电子邮件 [英] Batch Request - SendAs Emails

查看:60
本文介绍了批处理请求-SendAs电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以进行批量请求以从多个或所有用户那里获取SendAs电子邮件?

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

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

Currently we are using a service account with user impersonation to go through each user and get the SendAs email list - lot of requests.

  1. GmailService即服务-模拟为用户.
  2. service.Users.Settings.SendAs.List("me").Execute();

P.S.我在Google网上论坛中发布了此帖子,但刚读了一篇帖子,说该论坛现在是只读的!奇怪的是,它允许我发布新帖子(很明显,我认为该帖子必须得到批准)

P.S. I posted this in google group, but just read a post that said the forum is now read-only! It's weird that it allowed me to make a new post (and obviously i was thinking that the post has to be approved)

谢谢!

    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调用,并在此花费一些时间.

First I am gong to ask why you are using batching. If you are hoping it will save you on quota usage it wont batching is effected by the same quota usage as normal api calls. The only help batching will give you would be in sending fewer HTTP calls and there by spending things up a little.

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

Each HTTP connection that your client makes results in a certain amount of overhead. The some Google APIs supports batching, to allow your client to put several API calls into a single HTTP request.

外部批处理请求的HTTP标头(除Content-标头(如Content-Type)外)适用于批处理中的每个请求.如果在外部请求和单个调用中都指定了给定的HTTP标头,则单个调用标头的值将覆盖外部批处理请求标头的值.单个呼叫的标题仅适用于该呼叫.

The HTTP headers for the outer batch request, except for the Content- headers such as Content-Type, apply to every request in the batch. If you specify a given HTTP header in both the outer request and an individual call, then the individual call header's value overrides the outer batch request header's value. The headers for an individual call apply only to that call.

例如,如果您为特定呼叫提供 Authorization标头,则该标头仅适用于该呼叫.如果为外部请求提供Authorization标头,则该标头适用于所有单独的调用,除非它们用自己的Authorization标头覆盖它.

For example, if you provide an Authorization header for a specific call, then that header applies only to that call. If you provide an Authorization header for the outer request, then that header applies to all of the individual calls unless they override it with Authorization headers of their own.

授权

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

When you authorize to an api that authorization is for a single user.

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

上述服务只能访问一个用户数据来模拟您要模拟的用户.

The above service will have access to only that one users data the user which you are impersonating.

答案

是否可以进行批量请求以从多个或所有用户那里获取SendAs电子邮件?

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

不,没有.从上面可以看到,批处理请求的授权标头涵盖了该批处理中的所有项目.与GmailService一起为您的批处理请求创建的授权标头仅覆盖单个用户.

No there is not. As you can read from above the authorization header of a batch request covers all of the items in the batch. The authorization header created sent with the GmailService for your batch request is only going to cover a single user.

这篇关于批处理请求-SendAs电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆