【发布时间】:2021-12-11 15:27:03
【问题描述】:
我正在尝试访问 Google Directory api 作为测试此方法domains.list 使用服务帐户。
如果我使用我的域管理员电子邮件登录该页面上的 try me。它有效,我得到了回复。所以我传递的方法和客户 ID 应该可以工作。
我按照Perform Google Workspace Domain-Wide Delegation of Authority 此处的说明创建了一个服务帐户并启用了域范围的委派。
如果我同时检查我的工作区帐户和 Google 云控制台。委派似乎已配置。以我的域管理员电子邮件为原则。
我的代码:
namespace Daimto.Sample.WorkspaceAdmin
{
class Program
{
private static readonly string[] Scopes = {DirectoryService.Scope.AdminDirectoryDomain};
private static readonly string PathToServiceAccountKeyFile = @"C:\YouTube\workspaceserviceaccount-e4823a933ae3.json";
private static readonly string CustomerId = "C01lp3chxa";
private static readonly string workspaceAdmin = "xxx@daimto.com";
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
var credential = LoadGoogleCredentails();
var service = CreateDirectoryService(credential);
var request = service.Domains.List(CustomerId);
var result = request.Execute();
foreach (var domain in result.Domains)
{
Console.WriteLine(domain.DomainName);
}
}
private static DirectoryService CreateDirectoryService(GoogleCredential credential)
{
return new (new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Daimto Testing Workspace with service account"
}
);
}
private static GoogleCredential LoadGoogleCredentails()
{
return GoogleCredential.FromFile(PathToServiceAccountKeyFile)
.Impersonate(new ImpersonatedCredential.Initializer(workspaceAdmin))
.CreateScoped(Scopes);
}
}
}
错误
如上所示运行代码。
"error": { "code": 404, "message": "Requested entity was not found.", "errors": [ { "message": "Requested entity was not found.", "domain": "global", "reason": "notFound" }
未找到对我来说意味着它甚至无法访问该域。
但是,如果我删除模拟行。然后我得到这个错误
Not Authorized to access this resource/api [403] Errors [ Message[Not Authorized to access this resource/api] Location[ - ] Reason[forbidden] Domain[global] ]
这对我来说意味着如果没有模拟它就没有访问权限。
所以我很困惑。使用模拟找不到它,没有模拟它可以找到它但没有访问权限?
我在文档中能找到的唯一线索就是这个开始的注释。
那么,Only users with access to the Admin APIs can access the Admin SDK Directory API 我是管理员,我不应该有访问权限吗?如果需要,应该在哪里配置访问权限?
【问题讨论】:
标签: c# google-admin-sdk google-workspace google-api-dotnet-client service-accounts