从DirectoryService接收不足的权限错误

问题描述 投票:6回答:4

我正在尝试设置c#代码来管理您的Google域名。

每当我调用service.Users.List()或DirectoryService api中的任何其他方法时,我都会收到此错误。

Google.Apis.Requests.RequestError

Insufficient Permission [403]

Errors [

    Message[Insufficient Permission] Location[ - ] Reason[insufficientPermissions] Domain[global]

]

我按照OAuth设置上的所有说明进行操作。我使用的帐户是域管理员。

当我使用GAM.exe执行相同的操作时,我使用的客户端密码文件工作正常。这让我相信我在代码中做错了。

下面是我查询用户的代码,有什么我遗漏的吗?

        static void Main(string[] args)
    {
        var applicationName = "App Project Name";
        var userName = "[email protected]";
        var clientID = "clientIDfromAPIcredentialpageonconsole.developers.google.com";

        UserCredential credential;

        using (var stream = new FileStream("C:\\gam\\client_secrets.json", FileMode.Open, FileAccess.Read))
        {
            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                new[] { DirectoryService.Scope.AdminDirectoryOrgunit, DirectoryService.Scope.AdminDirectoryUser },
                userName,
                CancellationToken.None, null).Result;
        }

        var service = new DirectoryService(new BaseClientService.Initializer() 
            { 
                ApplicationName = applicationName, 
                HttpClientInitializer = credential 
            });

        var list = service.Users.List();

        var users = list.Execute();
    }
}
google-api-dotnet-client
4个回答
4
投票

2个选项:

  1. 您没有包含正确的范围。你确定DirectoryService.Scope.AdminDirectoryOrgunit,DirectoryService.Scope.AdminDirectoryUser就够了吗?
  2. 您是否在控制台中启用了API?有关更多信息,请访问:https://developers.google.com/api-client-library/dotnet/get_started#auth,在https://console.cloud.google.com/project中查找您的项目,并确保已启用Directory Admin API。

如果其中一个选项有效或者您仍然缺少其他选项,请更新此主题。


1
投票

领域

看来你正在尝试这个Quickstart

但是,该tuturoial中使用的范围是:

new [] { DirectoryService.Scope.AdminDirectoryUserReadonly };

但是,在您发布的代码的代码中:

new[] { DirectoryService.Scope.AdminDirectoryOrgunit, DirectoryService.Scope.AdminDirectoryUser },

令牌

更改范围(如上所示)后,您可能必须删除OAuth2令牌,然后重新授权应用程序的访问权限。 (除非你还没有完成“授权访问”步骤。)

enter image description here

\token.json\Google.Apis.Auth.OAuth2.Responses.TokenResponse-user

启用API

另外,正如我认为您已经发现的那样,启用Directory API与启用Gmail API(并在不同的URL中找到)的过程不同

Enable Directory API

enter image description here

Enable Gmail API

enter image description here


0
投票

这是我的工作凭证代码:

using (var stream =
    new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
    {
        string credPath = System.Environment.GetFolderPath(
            System.Environment.SpecialFolder.Personal);
            credPath = Path.Combine(credPath, ".credentials/calendar-dotnet-quickstart.json");

            UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                new string[] { CalendarService.Scope.Calendar },
                "[email protected]",
                CancellationToken.None,
                new FileDataStore(credPath, true)).Result;
            Console.WriteLine("Credential file saved to: " + credPath);
        }

确保在控制台中启用API,


0
投票

url https://developers.google.com/gmail/api/quickstart/dotnet上的doc将范围设置为static string [] Scopes = {GmailService.Scope.GmailReadonly};将其设置为GmailService.Scope.MailGoogleCom,然后继续使用文档中指定的流程。我在令牌respnse文件中编辑范围真是无聊

© www.soinside.com 2019 - 2024. All rights reserved.