Google Directory API - 使用服务帐户时出错400

时间:2013-10-04 19:33:28

标签: c# google-api google-oauth google-admin-sdk google-api-dotnet-client

我正在使用Directory API的最新Beta版,在尝试获取用户列表时,我得到以下信息:

“远程服务器返回错误:(400)错误请求。”

我可以从API资源管理器(https://developers.google.com/apis-explorer/#p/admin/directory_v1/directory.users.list)执行此功能,并且我已设置了所有适当的权限和范围。

下面的C#代码:

            X509Certificate2 certificate = new X509Certificate2(
                SERVICE_ACCOUNT_PKCS12_FILE_PATH,
                "KEY_PASSWORD", X509KeyStorageFlags.Exportable);

            var provider = new AssertionFlowClient(GoogleAuthenticationServer.Description, certificate)

            var provider = new AssertionFlowClient(GoogleAuthenticationServer.Description, certificate)
            {
                ServiceAccountId = SERVICE_ACCOUNT_EMAIL,
                Scope = Utilities.GetStringValue(
                    DirectoryService.Scopes.AdminDirectoryUser),
                ServiceAccountUser = SERVICE_ACCOUNT_USER,
            };
            var auth = new OAuth2Authenticator<AssertionFlowClient>(provider, AssertionFlowClient.GetState);

            var service = new DirectoryService(new BaseClientService.Initializer()
            {
                Authenticator = auth,
                ApplicationName = "API Project Name",                
            });

            var usersList = service.Users.List();
            usersList.Domain = "mydomain";

            Users results = usersList.Execute();

1 个答案:

答案 0 :(得分:1)

尝试按照Perform domain wide delegation of authority中的步骤操作。这些说明特定于Drive SDK,但可以轻松地适用于Admin SDK。特别关注the delegation step,经常会错过。