Gmail API,可使用C#访问,阅读/修改委托的电子邮件或共享的gmail

时间:2019-05-01 14:28:30

标签: c# asp.net gmail-api

我可以使用Gmail API的凭据通过我自己的Gmail帐户读取/修改Gmail。

using Google.Apis.Auth.OAuth2;
using Google.Apis.Gmail.v1;

但是现在,IT部门为我提供了一个共享的Gmail或委托Gmail帐户。因此,我想访问该代理电子邮件,并从中进行读取/修改

如何使用Gmail API访问和阅读代理gmail? 有例子吗?

我不理解,Gmail API网站中的大多数代码都使用Python和Java。

请点灯。

我尝试过的事情:

我可以使用Gmail API访问和阅读自己的邮件-

private static string[] Scopes = { GmailService.Scope.GmailModify };

private UserCredential _credential; 私有字符串credPath =“ token.json”;

 public UserCredential GetCredential()
 {
 using (var stream =
  new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
     {
         _credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
             GoogleClientSecrets.Load(stream).Secrets,
             Scopes,
             "user",
             CancellationToken.None,
             new FileDataStore(credPath, true)).Result;
     }

     return _credential;
 }

GmailCredentials Info = new GmailCredentials();
      private static string ApplicationName = "xxxxx";

        var service = new GmailService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = GetCredential(),
                ApplicationName = ApplicationName,
            });

            UsersResource.MessagesResource.ListRequest request = service.Users.Messages.List("me");
            request.Q = ConfigurationManager.AppSettings["filter"];
            request.MaxResults = Convert.ToInt64(ConfigurationManager.AppSettings["maxCount"]);  //5;
            messages = request.Execute().Messages;
            List<string> lstRemove = new List<string>() { "UNREAD" };

            /// Read the individual message and output as List<Email>
            for (int index = 0; index < messages.Count; index++)
            {
            //... Do the code...
            }

1 个答案:

答案 0 :(得分:0)

Gmail API不适用于委托邮箱。仅用户自己的邮箱可访问。要访问G Suite域中的所有用户邮件,请尝试使用服务帐户和domain-wide delegation