gpt4 book ai didi

c# - 无法使用 Google Directory API Admin SDK 列出用户

转载 作者:太空狗 更新时间:2023-10-29 18:18:32 25 4
gpt4 key购买 nike

我正在尝试使用 AdminService管理我的域的用户和组,但我坚持一个简单的请求来获取我的域的所有用户。 C#中有代码:

public Users GetAllUsers()
{
var provider = new AssertionFlowClient(
GoogleAuthenticationServer.Description,
new X509Certificate2(privateKeyPath, keyPassword, X509KeyStorageFlags.Exportable))
{
ServiceAccountId = serviceAccountEmail,
Scope = AdminService.Scopes.AdminDirectoryUser.GetStringValue()
};

var auth = new OAuth2Authenticator<AssertionFlowClient>(provider, AssertionFlowClient.GetState);

m_serviceGroup = new AdminService(new BaseClientService.Initializer()
{
Authenticator = auth,
});

var request = m_serviceUser.Users.List();
request.Domain = m_domainName;
return request.Fetch();
}

当 Fetch() 说:

Code: 403    
Message: Not Authorized to access this resource/api
Error: {Message[Not Authorized to access this resource/api] Location[ - ] Reason[forbidden] Domain[global]}

我已按照说明进行操作 here启用 API 访问,并在域控制面板中授权我的服务帐户:

[Security]->[Advanced Setting]->[Authentication]->[Manage third party OAuth Client access]

范围:

https://www.googleapis.com/auth/admin.directory.group 
https://www.googleapis.com/auth/admin.directory.user

Admin SDK 服务也在 API 控制面板中启用。

我尝试使用 DriveService 的代码并成功列出/创建/删除文件,没有任何问题,所以代码的身份验证部分应该没问题。我不知道还需要配置什么,或者我的代码是否还有其他问题。

感谢您的帮助。

最佳答案

如页面所述:

Manage API client access

Developers can register their web applications and other API clients with Google to enable access to data in Google services like Calendar. You can authorize these registered clients to access your user data without your users having to individually give consent or their passwords. Learn more

服务帐户需要根据用户的行为进行操作,因此在初始化客户端时需要分配 ServiceAccountUser。

    var provider = new AssertionFlowClient(
GoogleAuthenticationServer.Description,
new X509Certificate2(privateKeyPath, keyPassword, X509KeyStorageFlags.Exportable))
{
ServiceAccountId = serviceAccountEmail,
Scope = AdminService.Scopes.AdminDirectoryUser.GetStringValue(),
ServiceAccountUser = domainManangerEmail
};

编辑:AssertionFlowClient 已弃用,以下应该有效:

var cert = new X509Certificate2(privateKeyPath, keyPassword, X509KeyStorageFlags.Exportable);
var serverCredential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new []{DirectoryService.Scope.AdminDirectoryUser},
User = domainManagerAccountEmail
}.FromCertificate(cert));
var dirService = new DirectoryService(new BaseClientService.Initializer()
{
HttpClientInitializer = serverCredential
});

关于c# - 无法使用 Google Directory API Admin SDK 列出用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17033593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com