【问题标题】:Google.Apis.Requests.RequestError 404 - Error when using Gmail API to change signature for user on domainGoogle.Apis.Requests.RequestError 404 - 使用 Gmail API 更改域上用户的签名时出错
【发布时间】:2020-07-16 19:45:40
【问题描述】:

我想以 G Suite 管理员身份通过 Gmail .Net 客户端库更新域用户的签名。

我按照this 示例在谷歌开发者控制台中设置和授予权限。

这是我所做的总结:

  1. 在 Google API 控制台中启用 Gmail API
  2. 为我的项目创建了一个服务帐户
  3. 向服务帐户委派域范围的权限
  4. 添加了https://www.googleapis.com/auth/gmail.settings.sharinghttps://www.googleapis.com/auth/gmail.settings.basic 范围

我可以更新自己的电子邮件签名,但是当我尝试更新域中任何其他用户的签名时,我收到以下错误:

Google.Apis.Requests.RequestError
Not Found [404]
Errors [
    Message[Not Found] Location[ - ] Reason[notFound] Domain[global]
]

这是我的代码:

GoogleCredential credential;
using (var stream = new FileStream("signature-gmailapi.json", FileMode.Open, FileAccess.Read))
{
    credential = GoogleCredential.FromStream(stream).CreateScoped(new[]
            {GmailService.Scope.GmailSettingsBasic, GmailService.Scope.GmailSettingsSharing})
        .CreateWithUser("admin@domain.com");
}
var service = new GmailService(new BaseClientService.Initializer()
    {
        ApplicationName = "Project",
        HttpClientInitializer = credential
    }
);
service.Users.Settings.SendAs.Patch(new SendAs { Signature = "Test signature..."}, "me",
    "user@domain.com").Execute();

谁能指导我做错了什么?

【问题讨论】:

    标签: c# gmail-api google-admin-sdk


    【解决方案1】:

    要使用服务帐户更改域用户的签名,您需要使用impersonation

    这是通过CreateWithUser();方法实现的

    您需要分配给它的参数是用户的电子邮件,而不是管理员的电子邮件。所以:

    credential = GoogleCredential.FromStream(stream).CreateScoped(new[]{GmailService.Scope.GmailSettingsBasic, GmailService.Scope.GmailSettingsSharing})
            .CreateWithUser( "user@domain.com");
    

    【讨论】:

      猜你喜欢
      • 2017-11-21
      • 2023-04-05
      • 1970-01-01
      • 2017-08-21
      • 2019-05-14
      • 2021-09-21
      • 2018-07-30
      • 2017-03-03
      • 2016-07-31
      相关资源
      最近更新 更多