C# 请求的客户端未授权

C# 请求的客户端未授权,c#,asp.net,google-app-engine,google-api,google-apps,C#,Asp.net,Google App Engine,Google Api,Google Apps,我正在尝试使用谷歌服务帐户从我的域中获取谷歌用户 但这会带来错误 Error:"access_denied", Description:"Requested client not authorized.", Uri:"" 我的代码 X509Certificate2 certificate = new X509Certificate2(key_path, "notasecret", X509KeyStorageFlags.Exportable);

我正在尝试使用
谷歌服务帐户
从我的域中获取谷歌用户

但这会带来错误

Error:"access_denied", Description:"Requested client not authorized.", Uri:""
我的代码

X509Certificate2 certificate = new X509Certificate2(key_path,
                         "notasecret", X509KeyStorageFlags.Exportable);

ServiceAccountCredential credential = new ServiceAccountCredential(
           new ServiceAccountCredential.Initializer("publickey.gserviceaccount.com")
           {   Scopes = scopes,
               User = "admin@domain.com"
           }.FromCertificate(certificate));

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

service.Users.List().Domain = "domain.com";
Users results = service.Users.List().Execute();

提前感谢

服务帐户电子邮件地址需要访问域。将电子邮件作为用户添加,只要有足够的访问权限,它就可以阅读

您是否也更改了此项以便发布

"publickey.gserviceaccount.com"
服务帐户电子邮件看起来更像这样:

539621478854-imkdv94bgujcom228h3ea33kmkoefhil@developer.gserviceaccount.com

您需要首先让您的服务帐户/API项目访问您的域。 此处文档中详细说明的步骤:

您需要在这些说明的步骤6中指定所需的正确范围,即
https://www.googleapis.com/auth/admin.directory.user.readonly
访问用户列表


除此之外,要使目录API正常工作,您还需要在域设置中启用API访问:

我终于能够让它正常工作了。这是我的密码

        var grpReq = service.Groups.List();
        grpReq.Domain = "mydomain.com";
        Groups groups = grpReq.Execute();

        IList<Group> gps = groups.GroupsValue;

        var memReq=service.Members.List(groups.GroupsValue[0].Id);
        Members members = memReq.Execute();

你已经试过什么了?很多事情都可能出错,证书、证书的安装、您使用的用户等等。您是否授予服务帐户从您的域读取Google用户的权限?让我们再试一次。您是否已转到您的域并将服务帐户添加为授予其读取此信息权限的用户?我已在开发人员控制台中设置了此服务帐户的编辑权限。我仍然收到一个拒绝访问错误。如有任何帮助,我们将不胜感激。您提到的所有要点都已完成。我仍然收到拒绝访问错误。这个问题可能是由于我使用的DLL造成的-我从Google.api.Admin.Directory.Directory\u v1.Data下载了这个。是否有此DLL的更高版本?此处提供的所有最新客户端库文件,几天前有一个新的更新,但我认为这不应该是问题所在:只是为了确保设置步骤正确:在管理控制台中进行设置时,您必须使用
客户端ID
,它看起来像
randomstring.apps.googleusercontent.com
,对于ServiceAccountCredential,您必须使用类似于
randomstring@developer.gserviceaccount.com
是的,我已再次验证了这些步骤,但仍遇到此错误。您好,我已分配了如下证书文件路径--string path=“d:\\5e0938f59e0d3b88508670ce1c6b82065f26fbb0 privatekey.p12”;-- 这对不对?请核实。
        string mymail = googleauth.GetUsersEmail(ExchangeCodeWithAccessAndRefreshToken().Access_Token);

        string path = "d:\\c6b82065f26fbb0-privatekey.p12";
        X509Certificate2 certificate = new X509Certificate2(
            path,
            "notasecret", X509KeyStorageFlags.Exportable);

        ServiceAccountCredential credential = new ServiceAccountCredential(
          new ServiceAccountCredential.Initializer("876131792-v824u6drpss@developer.gserviceaccount.com")
          {
              User = mymail,
              Scopes = new[] { PlusService.Scope.UserinfoEmail, PlusService.Scope.UserinfoProfile, PlusService.Scope.PlusMe }
          }.FromCertificate(certificate));


        PlusService plus = new PlusService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "myapp"
        });

        Person profile = plus.People.Get("me").Execute();
        string email = profile.Emails[0].Value;