1
當我使用Directory API的最新測試版,並試圖讓我得到用戶的列表時,以下幾點:谷歌目錄API - 錯誤400使用服務帳戶
「遠程服務器返回錯誤:(400)錯誤的請求。「
我能夠從API Explorer(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();