【问题标题】:Email Audit API - 403 Forbidden电子邮件审核 API - 403 禁止
【发布时间】:2015-11-03 23:31:00
【问题描述】:

我正在尝试使用电子邮件审核 API 下载用户的邮箱。我收到对此代码的 403 Forbidden 响应(错误发生在最后一行,调用 UploadPublicKey 方法):

    var certificate = new X509Certificate2(System.Web.HttpRuntime.AppDomainAppPath + "key.p12", "notasecret", X509KeyStorageFlags.Exportable);

    ServiceAccountCredential credential = new ServiceAccountCredential(
       new ServiceAccountCredential.Initializer(serviceAccountEmail)
       {
           Scopes = new[] { "https://apps-apis.google.com/a/feeds/compliance/audit/" }
       }.FromCertificate(certificate));

    credential.RequestAccessTokenAsync(System.Threading.CancellationToken.None).Wait();
    DebugLabel.Text = credential.Token.AccessToken;

    var requestFactory = new GDataRequestFactory("My App User Agent");
    requestFactory.CustomHeaders.Add(string.Format("Authorization: Bearer {0}", credential.Token.AccessToken));

    AuditService aserv = new AuditService(strOurDomain, "GoogleMailAudit");
    aserv.RequestFactory = requestFactory;
    aserv.UploadPublicKey(strPublicKey);

我已经在 Developers Console 中创建了服务帐号,并在管理控制台中授予了 Client ID 对 https://apps-apis.google.com/a/feeds/compliance/audit/ 的访问权限。

在我看来,该帐户应该拥有所需的所有权限,但事实并非如此。知道我缺少什么吗?

【问题讨论】:

  • 您是否为您的服务帐户进行了域范围的委派? developers.google.com/drive/web/delegation 。您创建的服务帐户需要被授予对您要访问的 Google Apps 域用户的访问权限。
  • 我在管理控制台的“管理 API 客户端访问”下粘贴了服务帐户的客户端名称(例如 xxxxxxxxxxxx..apps.googleusercontent.com),然后输入了 API 范围apps-apis.google.com/a/feeds/compliance/audit 。但是,如何将服务帐户权限授予域用户?我想知道这是否不是电子邮件审核 API 的正确范围?
  • apps-apis.google.com/a/feeds/compliance/audit 是电子邮件审核 api 的正确范围。关于您的问题,您需要模拟用户,该用户将有权代表服务帐户执行请求。拨打电话时,应指定要模拟的用户。检查此处提供的一些示例developers.google.com/identity/protocols/OAuth2ServiceAccount
  • 我不能在域中为我的超级用户帐户获取访问令牌,然后使用该令牌调用电子邮件审核 API 吗?在我看来,这比每次需要下载用户邮箱时都必须向服务帐户授予模拟权限要容易,不是吗?
  • 我注意到了一些有趣的事情...我尝试通过 JsonConvert.SerializeObject(credential.Token); 查看令牌的内容我得到了这个: {"access_token":"yadayadayada","token_type":"Bearer","expires_in":3600,"refresh_token":null,"scope":null,"Issued":"2015-08-17T16 :07:02.9104014-05:00"} ...我想知道为什么当我在新的 ServiceAccountCredential.Initializer 中明确指定范围时,这里的范围为空...这是问题所在的线索吗?

标签: c# google-admin-sdk


【解决方案1】:

好的,所以我放弃了尝试使其与服务帐户一起使用,即使 Google 的文档会让您相信这是正确的方法。在向 Google 支持发送电子邮件后,我了解到我可以将 OAuth2 用于在开发者控制台上创建应用程序的超级用户帐户。

然后我按照此处概述的过程获取了用于脱机访问的访问令牌(刷新令牌): Youtube API single-user scenario with OAuth (uploading videos) 然后获取该刷新令牌并将其与以下代码一起使用:

public static GOAuth2RequestFactory RefreshAuthenticate(){
OAuth2Parameters parameters = new OAuth2Parameters(){
    RefreshToken = "<YourRefreshToken>",
    AccessToken = "<AnyOfYourPreviousAccessTokens>",
    ClientId = "<YourClientID>",
    ClientSecret = "<YourClientSecret>",
    Scope = "https://apps-apis.google.com/a/feeds/compliance/audit/",
    AccessType = "offline",
    TokenType = "refresh"
};
OAuthUtil.RefreshAccessToken(parameters);
return new GOAuth2RequestFactory(null, "<YourApplicationName>", parameters);
}

这是来自https://stackoverflow.com/a/23528629/5215904 的代码(除了我更改了倒数第二行...无论出于何种原因,在我进行更改之前共享的代码都不起作用)。

所以我终于能够获得一个访问令牌,允许我访问电子邮件审核 API。一旦我停止尝试使用服务帐户,一切都变得轻而易举。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多