Google.Apis.Requests.RequestError 404-使用Gmail API更改域上用户的签名时出错

问题描述 投票:0回答:1

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

我按照this示例在Google开发人员控制台中进行设置和授予权限。

这是我所做的摘要:

  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("[email protected]");
}
var service = new GmailService(new BaseClientService.Initializer()
    {
        ApplicationName = "Project",
        HttpClientInitializer = credential
    }
);
service.Users.Settings.SendAs.Patch(new SendAs { Signature = "Test signature..."}, "me",
    "[email protected]").Execute();

任何人都可以指导我做错了什么吗?

c# gmail-api google-admin-sdk
1个回答
0
投票

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

这通过方法CreateWithUser();实现

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

credential = GoogleCredential.FromStream(stream).CreateScoped(new[]{GmailService.Scope.GmailSettingsBasic, GmailService.Scope.GmailSettingsSharing})
        .CreateWithUser( "[email protected]");
© www.soinside.com 2019 - 2024. All rights reserved.