【发布时间】: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